Javascript document object can change the innerText property of the HTML elements like span, div, and paragraph etc using getElementById method. You can change the old innerText of the HTML span element or you can use the Javascript string concat function to concatenate the old text and new text into the span <span> tag. String concat function of javascript allows you to insert the new innerText in the beginning or at the ending of the previous text placed inside the <span> tag.
The innerText property of HTML elements such as <span>, <div> or <p> paragraph tag does not allow passing the HTML tags in the string value. Instead you can use "\n" backslash n in the string value as a newline character.
Here you have to specify the value of id attribute of <span> tag to access it using the getElementById method of document object. Value of id attribute must be unique in the current HTML document coz getElementById method returns reference to the first occurrence of HTML element with specified id.
document. getElementById("id") . innerText = "value";
innerText property of getElementById method gets or sets the value of innerText property value of the target HTML element.
<head>
<title>Javascript Change Span innerText</title>
<script language="javascript" type="text/javascript">
{
}
</script>
</head>
<span id="span1">
innerText placed inside the span element of HTML.<br />
Javascript will change the innerText of this HTML span element.
</html>
Similarly you can pass the id of any other HTML element such as <div> or <p> tag to change their innerText dynamically using Javascript function.
Be the first to rate this post
Tags: javascript, dom, javascript getelementbyid, javascript change innertext, javascript change innerhtml, html div tag, html p tag, html span tag, javascript dom, document object modeling, javascript innertext, javascript innerhtml, dhtml, dynamic html, javascript document object
8/23/2008 8:58:30 PM