Here you can find a number of examples on Java Script.Please find the various kinds of examples listed on the right side of this site. If you don't find anything on Java Script then please write to us or leave a commnet. We are pleased to help and find the solution of your problem.

JavaScript: splice Function



Splice Function:
Changes the content of an array,adding new elements while removing old elements.

Syntax:
splice(index,howmany,"element1","element2",......,"elementN");

Parameters:
index:Index at which to start changing the array.
howMany:An integer indicating the number of old array elements to remove. If howMany is 0, no elements are removed. In this case, you should specify at least one new element.
element1, ....,elementNThe element to add to the array. If you don't specify any elements, splice simply removes elements from the array.



Example in HEAD section:

<html>
<head>
<title>Splice Function</title>
<script language="javascript">
arr=new Array("one","two","three","Four");
document.write("<b>My Array before:</b> " + arr);
arr.splice(4,0,"five","six");
document.write("<br><b>my Array after:</b> " + arr);

document.write("<p><b>My Array before:</b> " + arr);
arr.splice(0,2,"1","2");
document.write("<br><b>my Array after:</b> " + arr);
</script>
</head>
</html>


Example in BODY section:

<html>
<head>
<title>Splice Function</title>
</head>
<body>
<script language="javascript">
arr=new Array("one","two","three","Four");
document.write("<b>My Array before:</b> " + arr);
arr.splice(4,0,"five","six");
document.write("<br><b>my Array after:</b> " + arr);

document.write("<p><b>My Array before:</b> " + arr);
arr.splice(0,2,"1","2");
document.write("<br><b>my Array after:</b> " + arr);
</script>
</body>
</html>



OUTPUT:
My Array before: one,two,three,Four
my Array after: one,two,three,Four,five,six

My Array before: one,two,three,Four,five,six
my Array after: 1,2,three,Four,five,six

No comments:

Post a Comment

Google