Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

js function to check class

Posted on 2011-03-08
3
Medium Priority
?
296 Views
Last Modified: 2012-05-11
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.getElementsByTagName("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].className,"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?

            
0
Comment
Question by:cycledude
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 8

Accepted Solution

by:
McNetic earned 2000 total points
ID: 35069302
You already put inputs[a].className into your function hasClass, where you again try to get the member className of it. You have to remove className in one of the places.
0
 

Author Comment

by:cycledude
ID: 35069392
Oh boy, that was such an easy fix!

thank you for pointing that out, I cannot believe I missed it!
0
 

Author Closing Comment

by:cycledude
ID: 35069395
;)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Explore the ways to Unlock VBA Project Password Excel 2010 & 2013 documents. Go through the article and perform the steps carefully to remove VBA Excel .xls file.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question