How do you "convert" $('#yourname').val(); to vanilla javascript? - javascript

Just need help getting the vanilla javascript version of $('#yourname').val(); part of var name=$('#yourname').val();

There are numerous selectors of JavaScript in order to do this.
You can find some here, here, and here.
<!DOCTYPE html>
<html>
<head>
<title></title>
<style>
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</head>
<body>
<input type="hidden" id="someID" value="625413">
<script>
var a = document.getElementById("someID").value;
var b = $("#someID").val();
var c = document.querySelector("#someID").value;
alert(a + " / " + b + " / " + c);
</script>
</body>
</html>
var a = document.getElementById("someID").value; is the 'classic' example of Vanilla JavaScript.

Related

Need to store html date input in JS var

I need help with JS and html. I need to store <input type = "date" id = "date"> in JS var. If you try to store the date in simple var and try to print it instead of your chosen date you get printed "undefined" :
$("#printDate").click(function(){
var dateVar = ($("#date").val());
$("#results").append("<p>" + dateVar + "</p>")
});
If you try to store the date in the dateVar using new Date() you get printed "Invalid date":
$("#printDate").click(function(){
var dateVar = new Date($("#date").val());
$("#results").append("<p>" + dateVar + "</p>")
});
Thank you for any kind of help!
upd: full page code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
`<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js">`</script>
</head>
<body>
<div id="results"></div>
<input type="date" id = "dateInput">
<button id = "printDate">PrintDate</button>
<script>
$("#printDate").click(function(){
var dateVar = new Date($("#date").val());
$("#results").append("<p>" + dateVar + "</p>")
});
</script>
</body>
</html>
$("#printDate").click(function(){
var dateVar = new Date($("#dateInput").val());
$("#results").append("<p>" + dateVar + "</p>")
});
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js">`</script>
</head>
<body>
<div id="results"></div>
<input type="date" id = "dateInput">
<button id = "printDate">PrintDate</button>
</body>
</html>
The only problem with your code is you are targetting wrong id i.e $("#date") instead of $("#dateInput")
<div id="results"></div>
<input type="date" id="dateInput">
<button id="printDate">PrintDate</button>
<script>
$("#printDate").click(function(){
var dateVar = new Date($("#dateInput").val());
$("#results").append("<p>" + dateVar + "</p>")
});
</script>
You have a typing error.

Why my jquery isn't displayed by browser?

so i am new to jq and i am trying to run this code but the browser doesn't show anything, tried to link the script library before ending tag of body.
Thanks!
<!DOCTYPE html>
<html>
enter code here
<head>
<meta charset="utf-8">
<title>Data Types</title>
<script src="jq/jquery-3.2.1.min.js"></script>
</head>
<body>
<script>
document.write(3.25 + 1000);
document.write('<br>' + 5 - 1000);
var1 = "Buna";
var2 = "Ziua";
document.write("<br>Concatenare siruri:" + var1 + var2 + '<br>');
a = true;
document.write(a);
var = fructe['mere', 'pere', 'caise'];
document.write('<br>' + fructe[1]);
</script>
</body>
</html>
You should fix this line:
var = fructe['mere', 'pere', 'caise'];
To
var fructe = ['mere', 'pere', 'caise'];

Changing document.write into the appropriate element.innerHTML

So, I'm new to learning js. And I know that document.write is outdated and the new solution is element.innerHTML. I'm working through some tutorial, but in testing my code it's breaking. What would be the appropriate means of going about inserting element.innerHTML and it's accompanying code, to get this to work?
Below is my snippet:
<!DOCTYPE html>
<html>
<head>
<title>8. Javascript Arrays</title>
</head>
<body>
<h1>8. Javascript Arrays</h1>
<script>
<div id="flightDate"></div>
<div id="myValues"></div>
var flightDate = new Date("September 22, 2004");
var myValues = ["Oceanic", 815, flightDate];
for (i in myValues)
{
document.write("<br />" + myValues[i]);
}
</script>
</body>
</html>
You can display the output in the DIVs that you created.
document.getElementById('flightDate').innerHTML = flightDate;
for (i = 0; i < myValues.length; i++) {
document.getElementById('myValues').innerHTML += myValues[i] + '<br/>';
}
+= concatenates the value with the content that's already there.

Google Feed API for RSS is not showing current events

I am attempting to use Google Feed API to display three current event listings for our homepage. This is to circumvent some of the issues we are having with a third part calendar application.
However, with the feed limit set to 3, the only listings that will show up for me are from Jan. 18. Is there a way to make the code show only current or future events?
Thanks in advance for any help.
HTML
<!DOCTYPE html>
<html>
<head>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
<meta charset="utf-8">
<title></title>
<script type="text/javascript" src="http://www.google.com/jsapi">
</script>
</head>
<body>
<div id="feed"></div>
</body>
</html>
JavaScript
google.load("feeds", "1");
var feedcontainer=document.getElementById("feed");
var feedurl="http://25livepub.collegenet.com/calendars/publishers-calendar-7.rss";
var feedlimit = 3;
var rssoutput = '';
function rssfeedsetup(){
var feedpointer=new google.feeds.Feed(feedurl);
feedpointer.setNumEntries(feedlimit) ;
feedpointer.load(displayfeed);
}
function displayfeed(result){
if (!result.error){
var thefeeds=result.feed.entries;
for (var i=0; i<thefeeds.length; i++){
var untrimContent = thefeeds[i].content;
var trimContent = untrimContent.split("<br>", 2);
rssoutput+="<div><a href='" + thefeeds[i].link + "'>" + thefeeds[i].title + "</a></div>" + trimContent;
feedcontainer.innerHTML=rssoutput;
}
} else {
feedcontainer.innerHTML = "Error Loading Events";
}
}
window.onload=function(){
rssfeedsetup();
};

javascript code working in jsfiddle but not working in the browser [duplicate]

This question already has answers here:
JSFiddle code not working in my own page
(3 answers)
Closed 8 years ago.
my code is working in jsfiddle but not working in the browser i dont know what i missed here could you please check it and tell me the solution. i google it but i am not getting correct solution please help me
this is jsfiddle link http://jsfiddle.net/pLTrJ/9/
<!doctype html>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"> </script>
</head>
<script type="text/javascript">
var random = 0;
var theDiv = document.getElementById("showVal");
updateTheDiv(9,0);
function updateTheDiv(inRange, inMin) {
random = (Math.random()*inRange+inMin)|0;
theDiv.innerHTML = random;
var nextCall = (Math.random()*1000)|0;
setTimeout(function() {
updateTheDiv(inRange, inMin);
}, nextCall);
}
</script>
<body>
<div id="showVal"></div>
</body>
</html>
jsfiddle has wrapped it automatically in the onload event. You haven't done this in your HTML page, so when it runs in the browser you're trying to get hold of the div before it's been rendered, so theDiv is null.
The below should work:
<!doctype html>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"> </script>
</head>
<script type="text/javascript">
window.onload = function () {
var random = 0;
var theDiv = document.getElementById("showVal");
updateTheDiv(9, 0);
function updateTheDiv(inRange, inMin) {
random = (Math.random() * inRange + inMin) | 0;
theDiv.innerHTML = random;
var nextCall = (Math.random() * 1000) | 0;
setTimeout(function () {
updateTheDiv(inRange, inMin);
}, nextCall);
}
}
</script>
<body>
<div id="showVal">
</div>
</body>
</html>
Try this in your Browser ;)
<!doctype html>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"> </script>
</head>
<body>
<div id="showVal"></div>
</body>
<script type="text/javascript">
var random = 0;
var theDiv = document.getElementById("showVal");
updateTheDiv(9,0);
function updateTheDiv(inRange, inMin) {
random = (Math.random()*inRange+inMin)|0;
theDiv.innerHTML = random;
var nextCall = (Math.random()*1000)|0;
setTimeout(function() {
updateTheDiv(inRange, inMin);
}, nextCall);
}
</script>
</html>
(I just put the script to the bottom. So the script loads after your element is set.)

Categories

Resources