0

I have the following code:

$("#tag-add-button").click(function () {
    var text = $("#tagadd").val();
    var tagcounter=0;
    $("#set-tags").append("<%= render 'tag' %>");
    $("#tag"tagcounter).val(text);
    $("#tagadd").val("");
    tagcounter=tagcounter+1;
});

Clearly its wrong, but here is what I'm trying to accomplish:

Someone clicks the tag-add-button, the text from tagadd is stored in the "text" var.

The variable tagcounter is set to 0

An input is rendered (within the set-tags div) containing an id such as "tag0", "tag1" etc.

The input ("tag0" etc.) then receives the value from the "text" var.

The original input box "tagadd" is then replaced with nothing ("").

Lastly the tagcounter variable increases by 1.

How can I make this work?

5
  • Variables can be used just like in normal JavaScript. Commented Feb 11, 2011 at 0:14
  • $("#tag"tagcounter).val(text); should be $("#tag" + tagcounter).val(text); Commented Feb 11, 2011 at 0:14
  • tagcounter=tagcounter+1; will not work i guess as you are initializing it with 0 on every click. You might want a global variable which you want to assign the value Commented Feb 11, 2011 at 0:17
  • @jitanmay alternatively store the tag counter on the dom object with .data. Best to avoid global state. Commented Feb 11, 2011 at 0:21
  • You realise that jQuery executes on the client-side? So $("#set-tags").append("<%= render 'tag' %>"); doesn't make any real sense? Commented Feb 11, 2011 at 0:24

1 Answer 1

2

Is this what you're trying to achieve?

var tagCounter = 0;
$("#tag-add-button").click(function(){
     var text = $("#tag-add").val();
     $("#set-tags").append(text);
    $("#tag"+tagCounter).val(text);
    $("#tag-add").val('');
    tagCounter++;
});

Not quite sure what you're trying to achieve in this line:

 $("#set-tags").append("<%= render 'tag' %>");

?

Sign up to request clarification or add additional context in comments.

2 Comments

@YngveBNilsen: I'm assuming the render 'tag' is some form of ASP macro for a TAG template--or so I would presume.
@Brad: Looks more like RubyOnRails to me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.