indexof() method
The indexOf() method gives the position of the first occurrence of a particular text in a string
Example
< !DOCTYPE html>
<p id="see">I am Very good person</p>
<button onclick="mydata()">result</button>
<p id="d"></p>
<script>
function mydata() {
var str = document.getElementById("see").innerHTML;
var pos = str.indexOf("good");
document.getElementById("d").innerHTML = pos;
}
</script>
</body>
</html>
OUPUT
10
lastindexof() method
The lastIndexOf() method gives the position of the last occurrence of a particular text in a string
Example
<!DOCTYPE html>
<html>
<body>
<p id="x">Please contact me</p>
<button onclick="myFunction()">result</button>
<p id="y"></p>
<script>
function myFunction() {
var str = document.getElementById("x").innerHTML;
var pos = str.lastIndexOf("c");
document.getElementById("y").innerHTML = pos;
}
</script>
</body>
</html>
OUPUT
12
0 Comment(s)