javascript - Matching a String and then incrementing a number within HTML elements -


I have the tag in the html list, it is an example of two tags.

  & lt; Div class = "tag" & gt; & Lt; Ul & gt; & Lt; Li & gt; & Lt; One onclick = "tag_search ('tag1');" Href = "#" & gt; Tag 1 & lt; Span class = "num-active" & gt; 1 & lt; / Span & gt; & Lt; / A & gt; & Lt; / Li & gt; & Lt; Li & gt; & Lt; One onclick = "tag_search ('tag2');" Href = "#" & gt; Tag 2 & lt; Span class = "num-active" & gt; 1 & lt; / Span & gt; & Lt; / A & gt; & Lt; / Li & gt; & Lt; / Ul & gt; & Lt; / Div & gt;  

I would like to write the function which I can pass a string that matches the hyperlink string i.e. "tag1" or "tag2", if there is a match Increasing the number in the period, if not, then add a new li.

I am getting a little worried how I can search for a string from class tags and then when I get a match, identify the element

I use it on JQuery Thank you for any help

Thanks all

H2> code so far

  function change_tag_count (item) {warning ( item); // string test $ .fn.searchString = function (str) {This return.filter ('*: contains ("' + object + '")';}; if ($ ('body'). Item) length {var n = $ ('a'). SearchString (item). Child () .text (); n = parseint (n) + 1;. $ ('One') searchString (item). Children () Text (N).} Other {Warning ('here'); // This does not warn when a li is not included in the word test $ ("# all_tags ul"). Append ('& lt; An onclick = 'tag_search (\' + + item + '\'); "Href =" # "& gt; '+ + Object +' & lt; span class =" num-active "> 1 & lt ; / Span & gt; & lt; / a & gt; ');}}  
< P>

The solution for you so far is very disabled. You can now have more luck using an object to keep track of the tags. , Such as:

  var tag = {}; // This tag will remember which we already have the function tag Search (text) {var $ match; if (tags.hasOwnProperty (text )) {$ Match = tags [text]; // us first use one}} and {$ match = $ (['& lt; Li & gt; & Lt; A href = "#" onclick = "tag search (\ '', text, '\'); return returned;" & Gt; ', Text,' & lt; Span class = "num-active" & gt; 0 & lt; / Span & gt; & Lt; / A & gt; & Lt; / Li & gt; '] .join (' ')). AppendTo ($ ('All_tags ul')); Tag [text] = $ match; // hold on for next time) var $ countSpan = $ match.find ('span.num-active'); Var count = parseInt ($ countSpan.text (), 10) + 1; $ CountSpan.text (count); }  

I'm not sure what your ultimate goal is with this code. Depending on what you want to do with it, there are probably many more elegant ways to write this code, which can not allow you to avoid taking olecunics on the elements, but the way you are already writing it Were connected to him. / P>

I tested this code, and it works according to your understanding of your needs.


Comments