Javascript "Undefined is not an object" when an object is defined? [duplicate] - javascript

This question already has answers here:
What do querySelectorAll and getElementsBy* methods return?
(12 answers)
Why does jQuery or a DOM method such as getElementById not find the element?
(6 answers)
Closed 1 year ago.
When selecting an ID or ClassName in Javascript it does not work. The class or ID does exist and is spelled correctly. Why does Javascript tell me that Undefined is not an object(evaluating 'header.style.height = ""') while the object is defined properly? The script below is just a plain example, but I can't select (almost) anything. No matter what ID or class I select. I know that it says it can't find the object, but why?
var header = document.getElementById("header");
window.onscroll = function() {scrollFunction()};
function scrollFunction() {
if (document.body.scrollTop > 128 || document.documentElement.scrollTop > 128) {
header.style.height = "48px";
} else {
header.style.height = "";
}
}

you can try to maybe change var header = document.getElementById("header")
to var header = document.getElementsByClassName("header")[0]
because i saw in your website that there is a div with a class name of "header" and you need to add the [0] to select the first element of the node list returned from getElementsByClassName.

Related

Making an input that adds to a list [duplicate]

This question already has answers here:
What do querySelectorAll and getElementsBy* methods return?
(12 answers)
Closed 1 year ago.
I am trying to make an input text and a button when pressed will add to a ul list but I keep having this error in the console and I don't know why
Uncaught TypeError: btn.addEventListener is not a function at script.js:5
Code :
var text = document.getElementsByClassName('text');
var list = document.querySelector('.list');
var btn = document.getElementsByClassName('add');
btn.addEventListener("click", function(){
var content = document.createElement('li')
content.innerText = text.value
list.append(content)
})
You are getting this error because getElementsByClassName returns an array of all elements having the given class. To solve this error you have two options:
Make add id instead of class and use getElementById to get that element.
var btn = document.getElementById('add');
If you do not want to make add id, we know getElementsByClassName returns an array you can access the elements inside this array by its index.
var btn = document.getElementsByClassName('add')[0];
In this case, keep in mind that the button you are trying to access with add class should be the first element with this classname.

jquery adding class to variable inside if, else statement [duplicate]

This question already has answers here:
How to add a class to a given element?
(28 answers)
Closed 2 years ago.
I am trying to add a class to a variable inside one of my if else statements in a jquery function. I am getting an error x.addclass is not a function. I have tried placing the variable in () and without. It makes no difference.
var ideal = document.getElementById( 'ideal_' + id )
var Search = document.getElementById('Search_' + id)
var parent = document.getElementById(+id)
if(ideal){
parent.addClass('myclass')
} else if (Search){
parent.style.background='yellow'
} else {
parent.style.background='none'
}
.addClass() is a jQuery method, you can call that on a jQuery referenced element.
Try using classList.add():
parent.classList.add('myclass');

How do i add class to parent from selected element in pure JS parentNode is undefined [duplicate]

This question already has answers here:
What do querySelectorAll and getElementsBy* methods return?
(12 answers)
Closed 2 years ago.
so hello i am trying to add class that for element that passes all selectors
this is my current code
document.querySelectorAll('.inner p .align_center').parentNode.classList.add('center_text')
which uhh should work, i am trying to translate this jquery code
$(".inner p .align_center").parent().addClass('center_text')
but i get .parentNode is undefined in the console why? i selected child of p with class of .alignt-center and i want to add class to that p element, or is there easier way of doing the same thing? what am i doing wrong
var a = document.querySelectorAll('.inner p .align_center');
for(var i = 0; i < a.length; i++) {
a[i].parentNode.classList.add('center_text');
}
just had to treat querySelectorAll as a array instead of single element

How can I check the document for an ID and change the style on a completely different class? [duplicate]

This question already has answers here:
What do querySelectorAll and getElementsBy* methods return?
(12 answers)
Closed 3 years ago.
I'd like to check the document for a specific ID. If the ID exists, I want to change the margin of a completely different class on the same page. I'm at a loss as to why it's not working. Maybe it's not even possible?
I've tried several different versions of the same type of coding below, so I'm guessing I'm way off base...
var a = document.getElementById('CommunityTabsContainer');
var b = document.getElementsByClassName('librarysearch');
if (a != null) {
b.style.marginTop = "-68px";
} else {
b;
}
You're on the right track. Assuming that CommunityTabsContainer is the id you want to scan for and librarysearch is the class you want to change; you can use document.querySelector to do what you need (documented Here). I would write something like this:
const a = document.querySelector('#CommunityTabsContainer')
const b = document.querySelector('.librarysearch');
if (a !== null) {
b.style.marginTop = '-68px'
}
Note: in your original code, getElementsByClassName returns a collection of elements, not a single element.

How to tell if an HTML element has a particular class in JavaScript? [duplicate]

This question already has answers here:
How can I check in JavaScript if a DOM element contains a class?
(8 answers)
Check if an element contains a class in JavaScript?
(30 answers)
Closed 9 years ago.
Is there an easy way to tell if an HTML element has a specific class? For example:
var element = document.getElementById('something');
if (element.class == 'car')
Of course an element can have multiple classes, so maybe the if statement would have to be of the following form?
if (element.class.includes('car'))
var element = document.getElementById("myid");
if (element.classList.contains("myclass")) { /* do stuff */ }
Read more on element#classList here: https://developer.mozilla.org/en-US/docs/DOM/element.classList
This link also contains a polyfill for older browsers.
If using jQuery is an option you can use hasClass() see http://api.jquery.com/hasClass/
If not you can take a look at pure JS implementation - Test if an element contains a class?
For strange reasons the name of the member containing the class(es) is className, not class. Multiple class names are separated by space.
You need to use:
class = document.getElementById("{id_of_element").getAttribute("class");
then
String[] vals = class.split(" ");
var match = false;
for (i = 0; i < vals.length;i++) {
if (vals[i].equalsIgnoreCase('car') {
match = true;
break;
}
}
if (match) {
//do something
}
HTH.

Categories

Resources