Introduction:
Indexof method of
jQuery is used to find out whether word
exists in the string or not. We can find out the first occurrence of word in
the string. This indexof method will return -1 incase if the word not exists in
string in JavaScript or jQuery and if exists it will return the position of the
word in string.
In this article I
will explain what is string indexof method in JavaScript or jQuery and uses of
indexof method in JavaScript or jQuery with example.
Declaration of indexof method to find the first occurrence of
word:
<script type="text/javascript">
$(function () {
$('#btncheck').click(function () {
var
str = 'welcome to Mazik Solutions';
var
strval = str.indexOf($('#txtword').val());
alert('Starting
Position of Word is ' + strval)
})
})
</script>
Declaration
of indexof method to check existence of Word:
<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>
Full Source code : Sample
Application
<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 Mazik Solutions';
var
strval = str.indexOf($('#txtword').val());
alert('Starting
Position of Word is ' + strval)
})
})
</script>
</head>
<body>
<form id="form1">
<center>
<fieldset style="width:300px">
<legend><strong>Indexof String Using jquery </strong></legend>
<div>
<br />
<b>Enter Text:</b><input type="text" id="txtword" value="" /><br /><br />
<input type="button" id="btncheck" value="Get Word Position" />
</div></fieldset></center>
</form>
</body>
</html>
0 comments:
Post a Comment