Javascript/HTML error cannot find variable [closed] - javascript

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
Via onclick I am calling a function that does some calculations and then should be replacing some elements (h1) on the fly with the information calculated within the function.
I have this done the same exact way on several other places on my page, except this one seems to be giving me an error.
ReferenceError: Can't find variable: container2
I believe it might have something to do with the element's ID.
HTML:
<div id="macros">
<h4>Macronutrients recommended:</h4>
<h2>Protein: <span id="myprotein"></span></h2>
<h2>Fats: <span id="myfat"></span></h2>
<h2>Carbs: <span id="mycarb"></span></h2>
</div>
Javascript:
function calcmacros(bmr){
var bmrweightvalue = document.getElementsByName('bmr_weight_pounds')[0].value;
var bmrweight = parseInt(bmrweightvalue, 10);
var macro_p = (bmrweight*1);
//window.alert("protein is: " + macro_p);
var macro_f = (bmrweight*.25);
//window.alert("Fats are: " + macro_f);
var macro_c = (bmr - (macro_p * 4 + macro_f * 9))/4;
//window.alert("carbs are: " + macro_c);
var contianer2 = document.getElementById("myprotein");
container2.innerHTML = macro_p;
var contianer3 = document.getElementById("myfat");
container3.innerHTML = macro_f;
var contianer4 = document.getElementById("mycarb");
container4.innerHTML = macro_c;
}
Console:
ReferenceError: Can't find variable: container2
calcmacrosrdnSimulator.html:435
onclickrdnSimulator.html:101
Line 435 is
var contianer2 = document.getElementById("myprotein");
as stated above

You are defining the container variables as contianer but then referring to them are container.
Change the variables to container and then it should work correctly.
var container2 = document.getElementById("myprotein");
container2.innerHTML = macro_p;
var container3 = document.getElementById("myfat");
container3.innerHTML = macro_f;
var container4 = document.getElementById("mycarb");
container4.innerHTML = macro_c;

You are calling a variable that doest exist. Check your syntax...
for example...
var contianer2 = document.getElementById("myprotein");
container2.innerHTML = macro_p;

Related

Missing parenthesis before function parameters [closed]

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 months ago.
Improve this question
I'm trying to update some old code for a customer but I keep receiving missing ( before function parameters in my log files.
function gmtToFloat(tc) {
timecode = tc.toString();
var hhd = parseFloat(timecode.slice(-6, -3));
var mmd = parseFloat(timecode.slice(-2)) / 60;
return (hhd + mmd);
}
var diff =
gmtToFloat({Data.Session.utc}) - gmtToFloat({Data.Session.ctc});
diff;
What am I doing wrong?
Here:
function gmtToFloat(tc) {
timecode = tc.toString();
var hhd = parseFloat(timecode.slice(-6, -3));
var mmd = parseFloat(timecode.slice(-2)) / 60;
return hhd + mmd;
}
var diff = gmtToFloat(Data.Session.utc) - gmtToFloat(Data.Session.ctc);
diff;
You have to change gmtToFloat({ Data.Session.utc }) to gmtToFloat(Data.Session.utc)

JS - Expected a conditional expression and instead saw an assignment [closed]

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 10 months ago.
Improve this question
The piece of code below seems to work in online code editors but when using on Cloud9 IDE, it comes up with that error message. Is there a way to sort this or alternatively write this bit of code?
The error appears on line 3, the for loop statement.
var text1 = document.querySelector('input[name="contract-type"]').value;
var select1 = document.getElementById('contract-type-list');
for(var i, j=0; i = select1.options[j]; j++){
if(i.text == text1){
select1.selectedIndex = j;
break;
}
}
The linter is warning you that i = select1.options[j] is a strange expression to be checking for truthyness, because it's an assignment. While you could ignore the rule, a better approach would be to iterate through the option children from querySelectorAll or .children or with the collection's iterator instead of going through .options[index].
var text1 = document.querySelector('input[name="contract-type"]').value;
var select1 = document.getElementById('contract-type-list');
for (const [i, option] of [...select1.options].entries()) {
if (option.text == text1) {
select1.selectedIndex = i;
break;
}
}
Or, if the value is definitely one of the options, just do
document.getElementById('contract-type-list').value = document.querySelector('input[name="contract-type"]').value;
or, if it might not exist:
const inputText = document.querySelector('input[name="contract-type"]').value;
const select = document.getElementById('contract-type-list');
const matchingOption = [...select.children].find(option => option.text === inputText);
if (matchingOption) {
select.value = matchingOption.ariaValueMax;
}

Uncaught TypeError: Cannot set property 'src' of null while changing SRC of img [closed]

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 5 years ago.
Improve this question
I want to when I click on IMG change his src.
This is my div:
<div class="character">
<input id="1200" name="vocation_select" type="radio" value="1200" style="display: none" ></input>
<label id="label_profesji" for="1200">
<img id="1200voc" onclick="onChange(this.id)" src="engine/images/proffesion/1200voc.png" width="34px" height="34px" style="cursor: pointer" title="Bardock">
</label>
And javascript:
<script>
function onChange(id){
var names = ("1200voc");
for (x = 0; x < names.length; x++) {
var prof = document.getElementById(names[x]);
prof.src = 'engine/images/proffesion/' + names[x] + '.png';
}
var prof = document.getElementById(id);
prof.src = 'engine/images/proffesion/' + prof.id + '_s.png';
};
</script>
The error is:
Uncaught TypeError: Cannot set property 'src' of null while changing SRC of img
Why this not working?
Looking at your code you do:
var names = ("1200voc")
then try to access names as an array.
So this:
for (x = 0; x < names.length; x++) {
var prof = document.getElementById(names[x]);
prof.src = 'engine/images/proffesion/' + names[x] + '.png';
}
Means that names[0] will return 1, names[1] will return 2, names[2] will return 0 and so on...
Now there's is no element with id of 1 or 2... so when you do:
var prof = document.getElementById(names[x]);
you get your error.
What you want is an array of names, so what you should do is:
var names = ["1200voc"]
This way you can properly loop through names as an array in the format you want.
Names isn't an array, it's a string.
Change your line to var names = ["1200voc"];

Javascript: Expected Identifier [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I am creating an HTML document and using javascript to create an image element.
Here is my code (create is already defined):
create=document.createElement("img");
create.src = 'data/1.png';
create.alt = 'image1';
create.style.magin = '1px';
eval("create.id = 'image" + count + "'");
create.class = 'block'; // line that breaks the code
document.body.appendChild(create);
I don't know what's going wrong here, but it's probably something obvious. Does anyone have any ideas?
Here's your code working with the variable count defined, an example image and the correct way to add a class to the element:
var count = 1;
create=document.createElement("img");
create.src = 'http://via.placeholder.com/350x150'; // 'data/1.png';
create.alt = 'image1';
create.style.magin = '1px';
eval("create.id = 'image" + count + "'");
create.classList.add('block');
document.body.appendChild(create);
As no necessity for eval and you can use classList for add a class.
for(var count = 1; count < 10; count++){
create=document.createElement("img");
create.src = 'data/1.png';
create.alt = 'image' + count;
create.style.magin = '1px';
create.id = 'image' + count;
create.classList.add('block');
document.body.appendChild(create);
}
You also can use create.style.display = 'block'; if you want just add a style.
More examples how set attributes and styles here

How to take users input, modify it, then display it in JQuery? [closed]

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
I am trying to make a unit converter, I will have the user enter a numeric value in a text box, select a unit type(lbs, g, oz etc...) from a select element, then do some simple multiplication to get the conversion, then finally, the part I am having trouble with, display the conversion result in a designated area. I can not get the text to change to the conversion result. I only have one conversion coded so far and that is pounds to grams, because I want to be able to display the text before I code the rest of them. Thank you and any input helps!
var main = function() {
var rslt = $('#result').val;
var num = $('#nmbr').val();
var inpt = $('#slct1').val();
var outpt = $('#slct2').val();
var bttn = $('.sbs');
$('.sbs').click(function(){
if(inpt == 'pounds'){
if(outpt == 'grams') {
var pGrams = num * 453.592;
$('#result').text(pGrams);
}
}
})
}
$(document).ready(main);
full code: https://jsfiddle.net/drzb6frk/
Try this javascript code:
var main = function() {
var bttn = $('.sbs');
$('.sbs').click(function(){
var rslt = $('#result').val;
var num = $('#nmbr').val();
var inpt = $('#slct1').val();
var outpt = $('#slct2').val();
if(inpt == 'pounds'){
if(outpt == 'grams') {
var pGrams = num * 453.592;
$('#result').text(pGrams);
}
}
})
}
$(document).ready(main);

Categories

Resources