JQuery Live Get Text Div

I have several radio buttons that are dynamically created using javascript. I need to figure out how to refer to text in a div (also generated dynamically) Here is an html example:

<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 button "shipping_service_2" is pressed, I would like to warn the text "14.00" - "shipping_service_price_2 id".

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 answers. The project code created incorrect html, so I could not select the text. The html above was just used for testing and actually works (yes, I'm moran)

+3
source share
3 answers

, , demo jsbin:

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

:

$("input[name=sid]")


, :

, . : jsbin.

+1

div, .text() .html(), (.. , div)...

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

Source: https://habr.com/ru/post/1767545/


All Articles