<div id="ChosenCategory" class="chosen">
<div class="cat_ch" name="1">
<div class="cat_ch" name="2">
<div class="cat_ch" name="3">
<div class="cat_ch" name="5">
<div class="clear"> </div>
</div>
I want to loop though div.cat_ch How?
This one fails:
$("div").each(function () {
alert("FW");
alert($(this).attr("name").val());
});
http://jsfiddle.net/2TRxh/
I think your issue lies with the attempt to get the val off the div after you get the attribute
$(this).attr("name").val(). Using.val()on a div doesn’t make sense. If you remove that$(this).attr("name")returns thenameproperty off the divs. You can further specify the div’s to loop through by using the class selector in your each rather than just div.$(".cat_ch").each(function () {});This has been shown in various other answers to this question.