views:

33

answers:

3

I have some radio buttons that are created dynamically with javascript. I need to figure out how to reference the text in a div (also created dynamically) Here is sample html:

<div>
 <div class="div_ceck"><input id="shipping_service_1" name="sid" value="1" type="radio"></div>
 <div class="free"><label for="shipping_service_1" id="shipping_service_price_1">Free</label></div>
 <br class="clr">
 <div class="div_ceck"><input id="shipping_service_2" name="sid" value="2" type="radio"></div>
 <div class="free"><label for="shipping_service_2" id="shipping_service_price_2">14.00</label></div>
</div>

So when the dynamic radio button "shipping_service_2" is clicked I would like to alert "14.00" - "shipping_service_price_2 id's text.

Here is my (non-working) code so far

$("input[name='sid']").live("change", function() {
   var id = $(this).val();
   alert($("#shipping_service_price_" + id).text())
  })

});

EDIT: Thanks for the responses. The projects code was producing some malformed html which is why I was unable to select the text. the html above was just used for testing and does in fact work (yes, I am a moran)

+1  A: 

just get the div reference and then use .text() or .html() to get the value (i.e., info stored in the div)...

Jason
+1  A: 
$("input[name='sid']").live("change", function() {
   var id = $(this).val();
   alert($("#shipping_service_price_" + id).html())
  })
});
YouBook
+1  A: 

Your code works perfectly well, once the unnecessary apostrophes are removed, demo at jsbin:

$("input[name='sid']")

should be:

$("input[name=sid]")


Edited, in response to double-checking my assumption:

I stand corrected, it works perfectly well with the apostrophes, too. Demo at: jsbin.

David Thomas
no, we can have apostrophes.
Ninja Dude
@Avinash: hence the second part of the answer, prior to your comment. Seriously, it's right there: just below the `<hr />` after the **Edited** bit.
David Thomas
at the time I commented, you haven't edited your post seriously :)
Ninja Dude
@Avinash, hmm, yeah, my five-minutes' edit-grace would've ended around the same time you made your comment. Fair dos, then, really =)
David Thomas