cycledude
asked on
js function to check class
Hi Experts
I have a js script which works well, that is until I have an object with 2 classes.
the script is created to re-style radio buttons, checkboxes and selects... written by ryan fait.
The script will style accordingly any radio,checkbox or select it comes across with the class='styled'.
in that scenario, it works great, however, if for example a radio button had
class='someClass styled'
the js will not register this, as it has 2 classes and the script is doing this:
var inputs = document.getElementsByTagN ame("input "), span = Array(), textnode, option, active;
for(a = 0; a < inputs.length; a++) {
if((inputs[a].type == "checkbox" || inputs[a].type == "radio") && inputs[a].className == "styled") {
my class='someClass styled' is not matching because it is not = 'styled'
so I need it to do a pattern match like this hasClass function
function hasClass(ele,cls) {
return ele.className.match(new RegExp('(\\s|^)'+cls+'(\\s |$)'));
}
trouble is when I run this
if((inputs[a].type == "checkbox" || inputs[a].type == "radio") && hasClass(inputs[a].classNa me,"styled "))
I get an error jn firebug....
"ele.className is undefined"
I am not familiar enough with js to know why this won't work, any ideas?
I have a js script which works well, that is until I have an object with 2 classes.
the script is created to re-style radio buttons, checkboxes and selects... written by ryan fait.
The script will style accordingly any radio,checkbox or select it comes across with the class='styled'.
in that scenario, it works great, however, if for example a radio button had
class='someClass styled'
the js will not register this, as it has 2 classes and the script is doing this:
var inputs = document.getElementsByTagN
for(a = 0; a < inputs.length; a++) {
if((inputs[a].type == "checkbox" || inputs[a].type == "radio") && inputs[a].className == "styled") {
my class='someClass styled' is not matching because it is not = 'styled'
so I need it to do a pattern match like this hasClass function
function hasClass(ele,cls) {
return ele.className.match(new RegExp('(\\s|^)'+cls+'(\\s
}
trouble is when I run this
if((inputs[a].type == "checkbox" || inputs[a].type == "radio") && hasClass(inputs[a].classNa
I get an error jn firebug....
"ele.className is undefined"
I am not familiar enough with js to know why this won't work, any ideas?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
;)
ASKER
thank you for pointing that out, I cannot believe I missed it!