PDA

View Full Version : changing a <label>


slyfox2099
10-04-2002, 08:45 PM
How do i change a label on the fly with onclick or onchange?

BrainJar
10-04-2002, 09:38 PM
Here's a simple function to replace the text in any element:


function replaceText(event, id, text) {

var el, textNode;

// get the element to update
if ((el = document.getElementById(id)) == null)
return;

// remove current contents
while (el.lastChild != null)
el.removeChild(el.lastChild);

// create a new text node
textNode = document.createTextNode(text);

// add text to the element
el.appendChild(textNode);
}

...

<label id="myLabel">Some text.</label>

...

<a href="" onclick="replaceText(event, 'myLabel', 'New text.'); return false;">change text</a>

slyfox2099
10-05-2002, 04:05 AM
:D Thanks for the help...i will try it. Thanks again.

slyfox2099
10-15-2002, 04:33 PM
I cannot get it to work with a textbox and onblur. Is there any reason as to why this doesn't work. I get this error:

"Unexpected call to method or property access"

landon11
10-15-2002, 06:53 PM
BrainJar,
How come you cannot just do it like this:


<HTML>
<head>
<script>
function replaceText(id, text){

document.getElementById(id).innerHTML = text;}

</script>

</head>
<body >
<label id="MyLabel">The Text.</label>
<a href="" onclick="replaceText('myLabel', 'New text.');">change text</a>

</body>
</HTML>

slyfox2099
10-15-2002, 07:23 PM
I am trying to update a section of for summary purposes and for asthetics I don't want to modify textboxes to do this like I am currently doing. The onclick isn't the preferred method I am looking for. I am wanting this to update on the fly while I am making selections on my form. I am just curious as to why the onblur gives an error and the onclick doesn't. I am fairly new with the DOM aspect of javascript, but am eagerly trying to learn it. Thanks for your help. If you think of a way to get it to work with onblur, please keep me informed.

BrainJar
10-16-2002, 03:34 PM
.innerHTML is a proprietary property. It will work but only on browsers that support it (IE). I always recommend using standard features so scripts will work on as many browsers as possible.

slyfox, can you post your code or a URL where it can be found?