Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

jQuery Replacing Text

I’m trying to replace the Quantity’s UOM text (Example: EA in the screenshot) with whatever label’s UOM is selected (Example: EA or CS6 in the screenshot).

enter image description here

Here’s the HTML code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<span class="uomPrice">
   <input type="radio" id="uom-0" name="adduom0" value="0">&nbsp;<label for="uom-0">$81.15&nbsp;EA</label><br>
   <input type="radio" id="uom-1" name="adduom0" value="1">&nbsp;<label for="uom-1">$486.89&nbsp;CS6</label>
</span>

<span class="qty-uom">EA</span>

Here’s the jQuery code:

$('.uomPrice label').click(function(e){
   var text = $(e.target).text();
   var selectedUom = text.substring(text.indexOf('\xa0') +1).toString(); // grabs uom after a non-breaking space
   var qtyUom = $('.qty-uom').text();
});

Thanks in advance!

>Solution :

Call .text() with an argument to replace the text.

$('.uomPrice label').click(function(e) {
  var text = $(e.target).text();
  var selectedUom = text.substring(text.indexOf('\xa0') + 1).toString(); // grabs uom after a non-breaking space
  $('.qty-uom').text(selectedUom);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<span class="uomPrice">
   <input type="radio" id="uom-0" name="adduom0" value="0">&nbsp;<label for="uom-0">$81.15&nbsp;EA</label><br>
   <input type="radio" id="uom-1" name="adduom0" value="1">&nbsp;<label for="uom-1">$486.89&nbsp;CS6</label>
</span>
<br>
<span class="qty-uom">EA</span>

Note that to make this work you have to click on the label, not the radio button.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading