Reputation: 843
First, this is probably a duplicate. Just couldn't find any answers that worked for me.
I have a block of elements like this
<a id="folder" href="http://example.com/update.php?dir=%2Fexample%2Fimg">
<i class="fa fa-folder-o"></i>
img
</a>
is there any way for jquery (or just js) to find the text inside there? I'd like it to return 'img' when given that setup.
Thanks in advance, Michael
Upvotes: 0
Views: 47
Reputation: 86
You can use string search() method.
var str = "This is test"; str.search("is")
will return you, first match of the regular expression inside the string, in this case 2.
Checkout
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/search
Upvotes: 0
Reputation: 40106
Let us know what else you want to do with this.
var all = $('a').text();
alert(all);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<a id="folder1" href="http://example.com/update.php?dir=%2Fexample%2Fimg">
<i class="fa fa-folder-o"></i>
img
</a>
<a id="folder2" href="http://example.com/update.php?dir=%2Fexample%2Fimg">
<i class="fa fa-folder-o"></i>
van
</a>
<a id="folder3" href="http://example.com/update.php?dir=%2Fexample%2Fimg">
<i class="fa fa-folder-o"></i>
project
</a>
<a id="folder4" href="http://example.com/update.php?dir=%2Fexample%2Fimg">
<i class="fa fa-folder-o"></i>
fun
</a>
<a id="folder5" href="http://example.com/update.php?dir=%2Fexample%2Fimg">
<i class="fa fa-folder-o"></i>
picnic
</a>
<a id="folder6" href="http://example.com/update.php?dir=%2Fexample%2Fimg">
<i class="fa fa-folder-o"></i>
car
</a>
<a id="folder7" href="http://example.com/update.php?dir=%2Fexample%2Fimg">
<i class="fa fa-folder-o"></i>
wagon
</a>
Upvotes: 0