Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have a div:
<div id="results"></div>
and in my js:
document.getElementById('results').innerHTML = "foo";
this works correctly however...
I try to store it...
var rslt = document.getElementById('results');
so I can use it more easily. However "rslt" is undefined and in firebug when I mouse over "results" inside the getElementById brackets it doesn't display any info. Like it's a string??
I'm sure this is probably very simple and I just can't see it...
When I call rslt it gives "null" now. But if I remove the "var reslt = " the rest of it "document.getElementById('results')" works perfectly and returns the div.
window.onload = function(){
var rslt = document.getElementById('results');
rslt.innerHTML = "This is working.";
};
Well, if it is undefined that means one simple thing - the object hasn't made it to the DOM tree yet.
Make that call after you're sure the div has been written to the document, e.g. after load event.
It is Working Perfectly..Look at the Fiddle
<div id="results"></div>
document.getElementById('results').innerHTML = "foo";
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
https://jsfiddle.net/c2o4j8fz/1/ - Where i get the code.
My code:
const chk = document.getElementById('chk');
const body = document.body;
$(function(){
chk.addEventListener('change', () => {
$('.body').toggleClass('dark');
localStorage.setItem("blockIsActive", $('.body').hasClass('dark'));
})
var blockIsActive = localStorage.getItem("blockIsActive")
if (blockIsActive == "true") {
$('.body').addClass('dark');
}
});
My code displays this error $ is not defined in the console, until i add jQuery, but in the jsfiddle example, it works in pure js. What am I doing wrong?
If you check the Resources tab of that fiddle, it actually says it includes jQuery:
Mind that $ isn't standard JavaScript, but a jQuery function/API to start with.
If you open Resources tab in the left menu you will see that jquery-2.1.4.min.js is included. So you have to include jQuery to get your code working.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
So when working on another question, here, I got a great answer from #earl3s and a link to a jsfiddle of the work and it appeared to work fine but when trying to run it in repl.it it gave me an error, and when trying to run it locally I got an error reading "Uncaught TypeError: Cannot read property 'add' of null" on line 19. I may be submitting them wrong or the code may be written incorrectly, and insight would be appreciated. Here is the origninal code:
<script>
if (window.location.href.substring(0,9) === "http://ww") {
var home_var = "bing.com";
}
else if (window.location.href.substring(0,9) === "https://p") {
var home_var = "https://px.multiscreensite.com/index.php?url=bing.com";
}
else {
var home_var = "1";
}
var select= document.getElementById("mySelect");
var option = document.createElement("option");
option.text = "Hope Page";
option.value = home_var;
select.add(option);
console.log(home_var);
</script>
<select id="mySelect" OnChange="window.location.href=this.value">
<option>Tagged Stuff and Navigation</option>
</select>
In JSFiddle, the JavaScript is automatically loaded after the document loads. In repl, you had the JavaScript inserted before the dom actually loaded, so mySelect didn't exist then. You have to add the JavaScript after the dom loads, i.e. at the end of the body tag.
Here's a working example.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I am using some simple javascript as below, but for some reasn the catParam is failing with error missing : after id. please help.
var catParam = "(id=cat00000)";
var inputParams = {serviceID:"getCategories",apiKey="asdfasfgx6",catCriterior:catParam};
Use
var catParam = "(id=cat00000)";
var inputParams = {serviceID:"getCategories",apiKey : "asdfasfgx6",catCriterior:catParam};
Instead - you are using an = instead of a : in your object literal. You assign properties of objects in literals using :.
Check out more info here.
Future reference
Try JsHint or JsLint to verify your code!
Also, if you have clean and organized code, it can make it easier to spot small errors like this, as well as improve the error messages you get (as your error will likely be on a shorter line). Using tools like JsBeautifier can get this done easily.
This would be your code after going through JS Beautifier:
var catParam = "(id=cat00000)";
var inputParams = {
serviceID: "getCategories",
apiKey: "asdfasfgx6",
catCriterior: catParam
};
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
var x = '<div><span></span><div id="container"></div></div>'
console.log($(x).find('#container').html())
I wonder why this doesn't work, I just want to extract the html before it's append to somewhere.
There is no html in #container. If you want to get the #container itself then you can do it with outerHTML like following.
var x = '<div><span></span><div id="container"></div></div>';
console.log($(x).find('#container')[0].outerHTML);
jQuery html api is for ( Get the HTML contents of the first element in the set of matched elements or set the HTML contents of every matched element. - http://api.jquery.com/html/)
your code is right, if you want to get "container" html, you have to call other function. e.g) get(0)
$(document).ready(function(){
var x = '<div><span></span><div id="container"></div></div>';
console.log($(x).find('#container').get(0));
});
if you want to add something or object, you can use append API.
$(document).ready(function(){
var x = '<div><span></span><div id="container"></div></div>'; // string
var $x = $(x); //$x is a jQuery object
$x.find('#container').append($('<div>test</div>'));
console.log($x);
});
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
In JavaScript code I use regular expression match() function to find pattern and show the result, but it does not give a proper answer, means finding [i] character in giving sentences
<button onclick = "searchPattern()">pattern</button><br>
<p id = "demo"></p>
<script>
function searchPattern(){
var str = "Visti W3Schools!";
var paat = /[i]/g;
var result = str.match(patt);
document.getElementById("demo").innerHTML = result;
}
</script>
Read your JavaScript console
Uncaught ReferenceError: patt is not defined
You changed your variable name from paat to patt half way through your code.