Navigation
Poll
Would you be an active poster if Ron's Guide had a message board?


Total Votes: 62
Comments: 18 — View
Past pollsPoll idea?
Rate Ron's Guide
Rate our resource at Bigwebmaster.com
Using deleteData()
Learn how to use the deleteData method to remove part of a string from a node of text.

The deleteData() method works like replaceData(), only that it excepts two parameters, where as the replaceData() method has a 3rd parameter which excepts a string value that is used in replacing text.

Here is an example of using "deleteData()"

<div id="mydiv">Here is some text</div>

<script type="text/javascript">
<!--

var div = document.getElementById("mydiv").firstChild;
div.deleteData(0, 8);

//-->
</script>

The goal is to delete the text "Here is " from the text node. The first job is to grab the div by using the "getElementById()" method, and then grab the first child (which is the text node we want to alter) using "firstChild".

Now that we have our text node, we now can use "deleteData()" to delete the text. The deleteDate() method excepts two parameters as stated above.

deleteData(start, end)

start is a number which indicates where to start the deleting from. Remember that indexing starts at 0, so the first letter in the string is "H". If you look at the example script above, and look at the start value, you will see it is 0. This is because I want delete "Here is " from the text node.

end indicates the length of the amount of characters you want to delete. So in this case, I set the length to eight.

So the text to be deleted now, would be the text in red.

Here is some text

You would then be left with "some text".

Discuss Tutorial: Using deleteData() 0 Comments
There are currently no comments on this discussion.
Post a comment
Sorry, you must be a registered member to post comments.

If you would like to register, you can do so here.
If you already have an account, please login.