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.
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 3 years ago.
Improve this question
I have no clue why this code doesn't work.
var tipCalculator = [1.2,1.15,1.10];
var restaurantBills = [124,48,738,10,300,150];
var finalBills = [];
function calculateBills(){
for (var i = 0 ; i < restaurantBills.length ; i++){
switch(true){
case restaurantBills[i]<50:
finalBills.push(restaurantBills[i]*tipCalculator[0]);
break;
case restaurantBills[i]>50 && restaurantBills[i]<200 :
finalBills.push(restaurantBills[i]*tipCalculator[1]);
break;
case restaurantBills[i]>200:
finalBills.push(restaurantBills[i]*tipCalculator[2]);
break;
default:
break;
};
};
return finalBills;};
console.log(calculateBills);
calculate bills is a function and so must be called
calculateBills();
then your code should be fine
As pointed by jonrsharpe you never called the function that is returning the array.
To make a function execute you need to call it.
change the line from
console.log(calculateBills);
to
console.log(calculateBills());
for debugging
Browsers like Chrome and Firefox comes with javascript debugger. Goto dev tools -> debugger where you would be able to set break points and trace the execution of your code.
Get Started with Debugging JavaScript in Chrome DevTools
How can I debug my JavaScript code?
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 4 years ago.
Improve this question
I have a code here:
$.each(localStorage, function(key, value){
if (key.indexOf('(cache)') > -1) {
var id = key.replace("(cache)", "");
id = '#' + id;
$(id).val(value);
}
});
My target is to update the value of the specific id but it gives an error of unkown expression (expected id here)
and also here is the image of the error
It's highly recommended to convert id properly (don't use dates and spaces). But if you can't then below is a solution
Working snippet:-
myval = 1;
id= '05/03/2018 15:39:51JxM8tX8K';
$('[id="'+id+'"]').val(myval);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="05/03/2018 15:39:51JxM8tX8K"><br>
<input type="text" id="05/03/2018 15:39:51JxM8tX8KDKHFCK">
Note:-I am not fully sure that it will work for you perfectly or not. Test it and let me know
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 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";