Introduction:
Here I will explain what is string indexof method in JavaScript or jQuery and uses of indexof method in JavaScript or jQuery with example. Indexof method in jQuery or JavaScript is used to check whether particular word existing in string or not and we can get position of first occurrence of word in string. This indexof method will return -1 incase if the word not exists in string in JavaScript or jQuery.
Description:
In previous articles I explained jQuery check if checkbox is checked or not, jQuery change opacity of image on mouseover, jQuery change image on mouseover, jQuery dropdown with images, jQuery show alert message while leaving from website, jQuery Countdown timer script example and many articles relating to jQuery and asp.net. Now I will use of string indexof method in JavaScript or jQuery.
In previous articles I explained jQuery check if checkbox is checked or not, jQuery change opacity of image on mouseover, jQuery change image on mouseover, jQuery dropdown with images, jQuery show alert message while leaving from website, jQuery Countdown timer script example and many articles relating to jQuery and asp.net. Now I will use of string indexof method in JavaScript or jQuery.
Declaration
of indexof method
Generally we will use indexof
function like as shown below
|
If
you want to check it in complete example we need to write the code like as
shown below
Sample1:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>jQuery Indexof String</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script type="text/javascript">
$(function()
{
$('#btncheck').click(function() {
var str = 'welcome to
aspdotnet-suresh.com';
var strval = str.indexOf($('#txtword').val());
if (strval > 0) {
alert('word
exists');
}
else {
alert('not
exists')
}
})
})
</script>
</head>
<body>
<form id="form1">
<div>
<b>Enter Text:</b><input type="text" id="txtword" value="aspdotnet" />
<input type="button" id="btncheck" value="Check" />
</div>
</form>
</body>
</html>
|
Live
Demo
To
check live demo try to click on below button to check whether particular word
exists or not in string 'welcome to
aspdotnet-suresh.com'
|
Sample2:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>jQuery Indexof String</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script type="text/javascript">
$(function()
{
$('#btncheck').click(function() {
var str = 'welcome to
aspdotnet-suresh.com';
var strval = str.indexOf($('#txtword').val());
alert('Starting
Position of Word is '+strval)
})
})
</script>
</head>
<body>
<form id="form1">
<div>
<b>Enter Text:</b><input type="text" id="txtword" value="aspdotnet" />
<input type="button" id="btncheck" value="Get Word Position" />
</div>
</form>
</body>
</html>
|
Live
Demo
To
check live demo try to click on below button to get stating position of particular
word in string 'welcome to
aspdotnet-suresh.com'
|
If you enjoyed this post, please support the blog below. It's FREE! Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email. |
|||
|
|||
0 comments :
Note: Only a member of this blog may post a comment.