How do I access the list of classnames using javascriptjQuery? - Stack Overflow

here are the html elements : <input class="myClass problemOne" type=text value=something&g

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

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

Share Improve this question edited Sep 29, 2010 at 11:51 NimChimpsky asked Sep 29, 2010 at 11:42 NimChimpskyNimChimpsky 47.3k61 gold badges201 silver badges317 bronze badges 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? – Bobby Borszich Commented Sep 29, 2010 at 11:46
  • What is the oute you are trying to achieve? – Jamie Taylor 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. – NimChimpsky Commented Sep 29, 2010 at 11:49
Add a ment  | 

4 Answers 4

Reset to default 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()
});

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

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./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>

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(','));

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744223395a4563890.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信