Why does `document.getElementById(“#datepicker1”)` not find my element? [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 7 years ago.
Improve this question
I am trying to show / hide a text input based on the selection of a drop down list.
Here’s a JSFiddle to what I am trying to do.
When I get into the function, I get a “TypeError: Cannot set property 'type' of null”.
This is the code:
function showCustomDate(val) {
console.log(val);
if (val == 4) {
var y = document.getElementById("#datepicker1");
//console.log(y.type);
y.type = "text";
}
else {
var y = document.getElementById("#datepicker1");
//console.log(y.type);
y.type = "hidden";
}
};

You do not need the # when using getElementById. getElementById returns null if does not find the element. null does not have a property named type - therefore you get the error.
function showCustomDate(val) {
console.log(val);
if (val == 4) {
var y = document.getElementById("datepicker1"); //removed #
//console.log(y.type);
y.type = "text";
} else {
var y = document.getElementById("datepicker1"); //removed #
//console.log(y.type);
y.type = "hidden";
}
};

y is null, because the id of the element is probably datepicker1 and not #datepicker1
var y = document.getElementById("#datepicker1");
should be
var y = document.getElementById("datepicker1");

Related

Why my if & else condition doesn't work correctly? [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 2 years ago.
Improve this question
When I use this code to add & remove the style , Part ( else ) only works and sets (display) to block, But it will not return the class if clicked again.
what's wrong?
var navDropDown = document.querySelectorAll('.menu-item-has-children > a');
for (let i = 0; i < navDropDown.length; i++) {
navDropDown[i].addEventListener('click', (e) => {
if (navDropDown[i].nextElementSibling.style.display = 'none') {
this.nextElementSibling.style.display = 'block';
} else {
this.nextElementSibling.style.display = 'none';
}
})
}
The line:
if (navDropDown[i].nextElementSibling.style.display = 'none') {
uses a single =, instead of ==. In Javascript, = is an assignment, which returns the value assigned. 'none' being not empty, it is converted into true, and thus the else will never be executed.

How to break the loop of element by id ().innerText is undefined [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 2 years ago.
Improve this question
I want to apply a condition where, if var bprodp = document.getElementById('ID_BPMPROD_' + rowIndex).innerText is undefined or null it should break the while loop and move to next part of code.
I tried if (bprodp = null) and (if bprodp = '') conditions but it always failed with an error "Unable to get property 'innerText' of undefined or null reference"
Can any one help me, how can i do this.
var count = 0;
var rowIndex = 1;
while (rowIndex <= 4){
var bprodp = document.getElementById('ID_BPMPROD_' + rowIndex).innerText
if (bprodp = null){
break;
}
var prodp = document.getElementById('ID_PROD_' + rowIndex).innerText;
rowIndex++;
if (bprodp != prodp) {
count++;
}
}
You should be checking the return value of document.getElementById(), not innerText. If the element exists, innerText will always be a string, never null.
while (rowIndex <= 4) {
var bprodp = document.getElementById('ID_BPMPROD_' + rowIndex);
if (!bprodp) {
break;
}
var prodp = document.getElementById('ID_PROD_' + rowIndex);
rowIndex++;
if (bprodp.innerText != prodp.innerText) {
count++;
}
}
Compare vales not assign:
If (bprodp === null){
....

jquery get not allowing x[i] inside of function [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 write a JQuery get that returns true if there is an embedded youtube video at the other end of the URL, and if so update the div to mention that.
My Code so far:
var x = document.getElementsByClassName("videoplace");
var lim=x.length;
for (var i = 0; i <lim; i++) {
$.get(x[i].innerHTML, function(data){if(data.indexOf("youtube.com/embed")>-1)
{
x[i].innerHTML = "Video Exists";
}
});
}
When I run this I get an error in the console that says "TypeError: Cannot set property 'innerHTML' of undefined" I have tried a plethora of alternatives and while the get function is working, and the if only fires if it returns true correctly, it doesnt correctly recognize where to put the "Video exists" text.
By the moment callback function(data) run for loop is over Correct call is
var x = document.getElementsByClassName("videoplace");
var lim=x.length;
for (var i = 0; i <lim; i++) {
(function(j){$.get(x[j].innerHTML, function(data){if(data.indexOf("youtube.com/embed")>-1)
{
x[j].innerHTML = "Video Exists";
}
}))(i);
}
x !== $x
var x = document.getElementsByClassName("videoplace");
var lim=x.length;
for (var i = 0; i <lim; i++) {
$.get(x[i].innerHTML, function(data){if(data.indexOf("youtube.com/embed")>-1)
{
x[i].innerHTML = "Video Exists";
}
});
}

Array.lenght returns undefined [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 8 years ago.
Improve this question
I wish to get the length of an array so I can use it further in another function but it returns undefined. This piece of code gets a file, opens it and splits it for each new line. I'm trying to get the length of the array but returns undefined.
function readBatFile(bfile){
var rawFile1 = new XMLHttpRequest();
rawFile1.open("GET", bfile, false);
rawFile1.onreadystatechange = function ()
{
if(rawFile1.readyState === 4)
{
if(rawFile1.status === 200 || rawFile.status === 0)
{
var allCode = rawFile1.responseText;
var ary = new Array;
ary = allCode.split(/.*\n/gm);
var rcount = ary.lenght;
document.getElementById('test').innerHTML = rcount;
}
}
};
rawFile1.send(null);
}
It is spelled length not lenght.
It should be:
var rcount = ary.length;

Obtaining a DOM element and adding event listener to it dynamically [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 9 years ago.
Improve this question
I have a function where I am dynamically trying to access a DOM element by passing it's id to a function and then adding an event listener to it,this code works perfectly outside a function but it does not work here.The function looks like this:
var count = 0;
function disableOnCheck(radioId, textBoxId) {
var a = document.getElementById(radioId);
var atext = document.getElementByID(textBoxId)
alert(a.value);
a.addEventListener('click', function (event) {
alert(count);
count++;
if (count === 2) {
a.checked = false;
atext.disabled = false;
count = 0;
return;
}
alert(a.value);
atext.disabled = true;
});
};
I run this using:
disableOnCheck('a','atest');
Also here is a fiddle that I created http://jsfiddle.net/vamsiampolu/vKkUP/2/
JavaScript is case-sensitive. It should be :
var atext = document.getElementById(textBoxId);
instead of
var atext = document.getElementByID(textBoxId);
You would have seen an error (Uncaught TypeError: Object # has no method 'getElementByID') in your console if you try to use it.
Update:
You are not getting what you want because you pass in the wrong variable in your
disableOnCheck function.
You have disableOnCheck('a', 'atest');
when it should be disableOnCheck('a', 'atext');
See Fiddle
change document.getElementByID to document.getElementById
First you are attempting to get the element Id by wrong methond, javascript is a case sensitive language
HERE IS A WORKING CODE:
var count = 0;
function disableOnCheck(radioId, textBoxId) {
var a = document.getElementById(''+radioId);
var atext = document.getElementById(''+textBoxId); // you were using getElementByID which is wrong
alert(a.value);;
a.addEventListener('click', function (event) {
alert(count);
count++;
if (count === 2) {
a.checked = false;
atext.disabled = false;
count = 0;
return;
}
alert(a.value);
atext.disabled = true;
});
};
disableOnCheck('a','atext'); // HERE you were using `atest` which is not a element id it's `atext`
The fiddle link is here.

Categories