2

here are the html elements :

<input class="myClass problemOne" type=text value=something></input>
<input class="myClass problemTwo" type=text value=somethingelse></input>

and here is the javascript and jquery in which I am tryign to access classaname, neither method a, or b works ... "classname is null or not an object".

var myElems = $(this).find(".myClass");

myElems.each(function(index) {
   var classLista = $(this).className.split(/\s+/);
   var classListb = myElems[index].className.split(/\s+/);
   var classListc = this.className.split(/\s+/);

   categories[index].key = classlist[1]; 
   categories[index].value = $(this).value();   
});

The end goal is to have problemOne and problemTwo returned as string and then stored in an array, which then go to the database blah blah blah.

Thanks a lot for any input !

edit explained end goal

3
  • do you want the list of class names for one element? i.e. "myclass" and "problem" or the list of classes for all elements? Commented Sep 29, 2010 at 11:46
  • What is the outcome you are trying to achieve? Commented Sep 29, 2010 at 11:46
  • a lsit of classnames for each element. There are only two, but the second can vary, shown in my edit. Commented Sep 29, 2010 at 11:49

4 Answers 4

7

You can use jQuery's .attr() method to get any attribute, including classes.

$(this).find('.myClass').each(function(index) {
    var element = $(this), classList;
    classList = element.attr('class').split(/\s+/);

    categories[index] = {
        key : classList[1],
        value : element.value()
    }
});

Aside:
if categories[index] already contains other info, you could use $.extend()

$.extend(categories[index], {
    key : classList[1],
    value : element.value()
});
Sign up to request clarification or add additional context in comments.

1 Comment

while this does achieve the goal, it doesn't answer the larger question of why IE7 in particular interprets $().eq(x)[y].classList[n] as .classList.n; furthermore, why classList is undefined in IE(7). Clearly .1, .2, etc, are insufficient object names. Why do jQuery objects have this property built into them if it appears it cannot be accessed in IE7? (at least - i explored this in Chrome and IE7 only.) In fact, $().eq(x)[y].classList comes up after a type check as "undefined", in IE7, but is a normal object in Chrome. Any ideas?
3

The property is called className. This is a property of a DOM element, not a jQuery element, so this.className should work.

Comments

1

Just use the attr method and split:

<html>
<body>
<input class="myClass problem" type=text value=something></input>
<input class="myClass problem" type=text value=somethingelse></input>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>
<script>
   //<!--
    $(document).ready(function(){
        var Classes = $('input').attr('class').split(' ');
        alert(Classes[0] + ' - '+  Classes[1]);
    });
    //-->
</script>
</body>
</html>

Comments

0

in POJ:

var elms = document.getElementsByTagName("INPUT"), 
    i = elms.length,
    arr = [],
    classArray;

while​(i--) {
    classArray = elms[i].className.split(/\s+/);
    if (classArray.length > 1) {
        arr.push(classArray[1]);
    }
}
alert(arr.join(','));

2 Comments

your second declared variable i will throw an error because elms is not yet defined.
I think you should try it first. It is declared because "," separates variables--it's a single var statement defining each variable in the list.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.