Question

I'm looking to get the value of a custom attribute in an image by class, using next().

$("#chkSell").live('change',function(){
     var posType = $(this).next('img[class="qtyImgDD"]').attr('posType');
});
<div class="Sell" style="width: 47px;"><input type="checkbox" fund-id="1" id="chkSell" class="_1"/></div>
<div class="Buy" style="width: 47px;"><input type="checkbox" fund-id="1" id="chkBuy" class="_1"/></div>
<div class="BuySelllbl" style="width: 10px;"><label class="lblBuySell_1" fund-id="1">&nbsp;</label></div>
<div class="Amt" style="width: 116px;"><input type="textbox" fund-id="1" id="AmtValue" size="8" disabled="disabled"/></div>
<div class="QtyType" style="width: 55px;"><label class="lblQtyType" fund-id="1" ddclass="_1">D</label>&nbsp;

<img src="/Applications/Images/ModelManagement/DropDown_Disabled.gif" fund-id="1" posType="MutualFund" class="qtyImgDD" disabled="disabled" ddclass="_1"/></div>

What i want is when someone clicks on the chkSell checkbox and enacts the change() function to get the posType attribute value from the on the bottom?

Was it helpful?

Solution

There are a couple minor things that you may want to change. Switch "live" to "on", switch "next" to "nextAll", change "img[class="qtyImgDD"]" to img.qtyImgDD.

$("#chkSell").on('change',function(){
     var posType = $(this).parent().nextAll('.QtyType').find('img.qtyImgDD').attr('posType');
});

Next is only the immediate sibling, where as nextAll is all following siblings.

Also, the markup you have doesn't have the elements you're targeting as direct siblings. So you'll need to traverse through their parents.


Ref:

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top