Javascript change text on label with for

185
January 29, 2018, at 9:01 PM

Hi i need change text via Javacript in this labels. Labels have not class or id, only atribute for. Can you help me why?

<label for="phone">Phone<span class="red"><strong>*</strong></span></label>

I try this, but this change text in all labels at form.

<script type="text/javascript">
$(document).ready(function(){ $('label').html('Phone to work'); });
</script>

Thank you

Answer 1

You can select it using the attribute and value...

$(document).ready(function() {
    $('label[for=phone]').html('Phone to work');
});
Answer 2

You can use get the target the <span> element, then use previousSibling to get the TEXT_NODE then set it value

$(document).ready(function() { 
  var spanDOMElement = $('label[for=phone] span.red').get(0); //Get the refernce to DOM element 
  spanDOMElement.previousSibling.nodeValue = 'Phone to work'; 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
<label for="phone">Phone<span class="red"><strong>*</strong></span></label>

Answer 3

As per your need i think you can try this to solve your problem

<label for="phone">Phone<span class="red"><strong>*</strong></span></label>
<br>
<label for="cell">Phone<span class="red"><strong>*</strong></span></label> 
$(document).ready(function(){ 
    $("label[for=phone]").text('Phone to work'); 
  });
Rent Charter Buses Company
READ ALSO
Suggestions for Jquery plugins to create pivot tables and charts at the same time [on hold]

Suggestions for Jquery plugins to create pivot tables and charts at the same time [on hold]

I am looking for a powerful Jquery plugin for creating pivot tables and charts similar to flexmonster solution, unfortunately it's paid

199
Uncaught TypeError: soliloquy_14.soliloquy is not a function

Uncaught TypeError: soliloquy_14.soliloquy is not a function

I have a problem width Soliloquy and I don't know how to fix this:

250
how to calculate growth chart percentile with the use of z-score [on hold]

how to calculate growth chart percentile with the use of z-score [on hold]

I want calculate the percentile of growth chart as like enter image description here

240
full syntax for gradle task declaration

full syntax for gradle task declaration

I'm currently learning how to use gradle task API to build java projects

335