Tuesday 15 July 2014

making a variable a class in javascript -


In my application, I have some tags that look like this

x Beer x wine x sticky beer

If you click on 'X' then this tag should be deleted.

With a link around 'X', as you have seen below, this tag has been created & lt;% =% & gt; Code is Ruby inside, for example, the Paramarijet method will make the sticky beer tag like 'Gummi-bear'.

  & lt; A href = "#" class = "link tag" data-span = "& lt;% = tag.name.parameterize%>" data-questions = "<% = @ question.id% & gt; "Data-tag =" & lt;% = tag.name%> & gt; & Gt; X & lt; / A & gt; & Lt; Span class = "& lt;% = tag.name.parameterize%>" & Gt; & Lt;% = tag.name% & gt; & Lt; / Span & gt;   

I have javascript click events installed on the 'X' link, I can immediately remove the 'X' on the click, but I can not remove the actual tag name until a page refresh Which is what i want To immediately remove the tag name, I created a span class around the tag name, which is one of the html data attributes on the 'x' link. I have saved the data attribute in a variable (this.span)

  this.spanVariable = $ (r.target) .attr ("data-span"); For example, if this sticky bear tag was clicked on, then spanVariable will now be 'sticky-bear'.   

/ P>

And then I would like to empty the html of the 'Gummy-Bear' class, so I want to do something to evaluate the JavaScript variable so that this class Be made I thought I can evaluate JavaScript inside # {} but I do not think: (

  $ ('. {{This.spanVariable}'). Html ('') ;   

What I am trying to do, is basically a way to evaluate the value, this can be a class which is also a jquery object (i Jquery manner).

Try it out:

  $ ('.' + This.spanVariable).   

Running this way Avoid the situation, call some Ajax, and do the stuff when he gives it:

  $ ('.xclass') .click (function (e) {var spanVariable = $ (This) .data ("span"); $ .ajax ("/ my / server / endpoint? Tag =" + spanVariable, {dataType: 'json', success: function (result) {$ ('.' + Result .spanVariable) .remove (); $ ['[data-span =' + result.spanVariable + ']'). Remove ();}, Error: function () {Console.log ('not removed tag Could go ... ');}}});    

No comments:

Post a Comment