Hi
I have two buttons in aspx file and i want to use document.getElementsByClassName in javascript to reach them all in one time.
The problem is that class doesn't exist for button (only CcsClass) nor something like document.getElementsByCssClassName.
How can i fix this?
Thanks and happy new year.
for (i = 0; i < 2; i++) {
var a1[i] = document.getElementsByClassName("menu");
a1[i].onmouseover = txtover
a1[i].onmouseout = txtout
}
function txtover() { this.style.color = "#BCAEA5" }
function txtout() { this.style.color = "black" }
Sachin SinghPosted Jan 1, 2023, 6:13 PM
Can you please inspect the element and check in browser ?
I think cssclass also compiles to normal class, so for browser both are the same.
directly copy paste below code in your browser's console and check are you getting an array or not?
if yes then try to use addeventlistner.
Valerie MeunierPosted Jan 2, 2023, 10:38 AM
Ok, my fault was this: var a1[i] = document.getElementsByClassName("menu");
This code works:
var a1 = document.getElementsByClassName("menu");
a1[i].onmouseover = txtover
a1[i].onmouseout = txtout
Conclusion: Sachin Singh was right: class is the same as CssClass and the code of Pasang Tamang is also correct.
Thanks
Pasang TamangPosted Jan 2, 2023, 10:21 AM
Hi,
Still, if you want to achieve this in Javascript for learning purpose then you have to change your code a bit. Below is the corrected code. Since you are using getElementsByClassName, indexing should be with element, not variable.
Regards,
Pasang
Sachin SinghPosted Jan 2, 2023, 10:14 AM
Can you please inspect the element and check in browser, means
Run the application.
On browser select the particular element (Button) by dragging and Right click and inspect , check what is the class name there?
Javascript is all about browser, so if you have any doubt directly run the code on browser's console.
Valerie MeunierPosted Jan 2, 2023, 10:05 AM
Thanks all for replying. I knew the solution with CSS, but i'm trying tot do this with javascript as exercise. Suppose i want to change the height of all buttons or something like that when clicking on it.
I don't understand very well what Sachin Singh means (Can you please inspect the element and check in browser?)
Thanks again
Jignesh KumarPosted Jan 2, 2023, 5:29 AM
Hello,
Its better to use css which will help you to maintain unique layout for your all button for long run. As pasang Tamang added class in css you can create in css which will work for entire project where you want to use menu class and easy to main rather then javascript or jquery.
Puneet KankarPosted Jan 1, 2023, 4:48 PM
you can use simple class instead of cssclass . Try below code.
Pasang TamangPosted Jan 1, 2023, 3:29 PM
Hi,
I suggest to use CSS for this. CSS is much better than handling in Javascript. Your CSS should looks like below.
Regards,
Pasang