Skip to content Skip to sidebar Skip to footer

Changing Label Content Using Javascript

Is it possible to change label content using javascript?

Solution 1:

Give the label an ID and change it like this:

<label id='label-id'>How is it?</label>
<input type="button" value="change label"
       onclick="document.getElementById('label-id').innerHTML = 'Like this!';"/>

(Forgive the inline event handler. :-) )

Working demo here: http://jsfiddle.net/bd349/


Solution 2:

Can you use jQuery?

$('label').text("whatever you want here");

With JS:

document.getElementsByTagName('label')[0].innerHtml = "whatever you want here";

It would be best to give it an ID so you can target it directly.


Solution 3:

Based on what you have posted, this code would work:

var label = document.getElementsByTagName('label')[0], // first label
input = document.getElementsByTagName('input')[0]; // first input

// attach click handler
input.onclick = function() {
    label.innerHTML = 'changed label';
};

Post a Comment for "Changing Label Content Using Javascript"