Matching a String and then incrementing a number within HTML elements
Posted
by Abs
on Stack Overflow
See other posts from Stack Overflow
or by Abs
Published on 2010-05-17T18:25:12Z
Indexed on
2010/05/19
4:20 UTC
Read the original article
Hit count: 158
jQuery
|JavaScript
Hello all,
I have tags in a html list, here is an example of two tags.
<div class="tags">
<ul>
<li>
<a onclick="tag_search('tag1');" href="#">tag1
<span class="num-active">1</span></a>
</li>
<li>
<a onclick="tag_search('tag2');" href="#">tag2
<span class="num-active">1</span></a>
</li>
</ul>
</div>
I would like to write a function that I can pass a string to, that will match the strings in the a hyperlink i.e. "tag1" or "tag2", if there is a match then increment the number in the span, if not then add a new li.
The bit I am having trouble with is how do I search for a string in the div with class tags and then when I find a match identifying the element. I can't even do the first bit as I am use to using an ID or a Class.
I appreciate any help on this using JQuery
Thanks all
Code so far
function change_tag_count(item){
alert(item);//alerts the string test
$.fn.searchString = function(str) {
return this.filter('*:contains("' + item + '")');
};
if($('body').searchString(item).length){
var n = $('a').searchString(item).children().text();
n = parseInt(n) + 1;
$('a').searchString(item).children().text(n);
}else{
alert('here');//does not alert this when no li contains the word test
$("#all_tags ul").append('<a onclick="tag_search(\''+item+'\');" href="#">'+item+'<span class="num-active">1</span></a>');
}
}
© Stack Overflow or respective owner