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 SinghRank #66 · 26.2K reputationPosted Jan 1, 2023, 1:13 PM EST
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?
document.getElementsByClassName('menu');if yes then try to use addeventlistner.
Pasang TamangRank #449 · 5.6K reputationPosted Jan 1, 2023, 10:29 AM EST
Hi,
I suggest to use CSS for this. CSS is much better than handling in Javascript. Your CSS should looks like below.
.menu{ color: black; } .menu:hover{ color: #BCAEA5; }Regards,
Pasang
Puneet KankarRank #446 · 5.6K reputationPosted Jan 1, 2023, 11:48 AM EST
you can use simple class instead of cssclass . Try below code.
//css .menu{ color: black; } .menu:hover{ color: #BCAEA5; }Jignesh KumarRank #91 · 22.9K reputationPosted Jan 2, 2023, 12:29 AM EST
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.
Valerie MeunierRank #1,526 · 1K reputationPosted Jan 2, 2023, 5:05 AM EST
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
Sachin SinghRank #66 · 26.2K reputationPosted Jan 2, 2023, 5:14 AM EST
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.
Pasang TamangRank #449 · 5.6K reputationPosted Jan 2, 2023, 5:21 AM EST
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.
for (i = 0; i < 2; i++) { var a1 = document.getElementsByClassName("menu")[i]; a1.onmouseover = txtover; a1.onmouseout = txtout; }Regards,
Pasang
Valerie MeunierRank #1,526 · 1K reputationPosted Jan 2, 2023, 5:38 AM EST
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