0

So I have a variable that I am declaring at the top of the JS document:

var poll_answer =  2;

However later on in the code, I am trying to append a new element with jQuery and would like to make use of this variable albeit adding '1' each time it is appended.

This is the main function:

function add_poll_answer_add() {
jQuery('#poll_answers').append('<tr id="poll-answer-" Answer' + poll_answer+1 + '>
    }

By default there are two 'answers' and hence the reason for setting the var at 2. But whenever someone appends a new element through a button, I would like them to see a new <tr> with the content Answer 2, 3, 4 etc. as they add more answers.

I get no errors in the debug console.

Hope this makes sense.

4
  • 2
    ... Answer' + (poll_answer++) + '> ... you need to increment the counter variable itself, not just add to a static value. Commented Feb 19, 2016 at 8:31
  • @Emissary has the Correct answer. Commented Feb 19, 2016 at 8:34
  • more like (++poll_answer) to get the number the question was trying to get Commented Feb 19, 2016 at 8:35
  • Ok thanks I'll give that a shot. Commented Feb 19, 2016 at 8:38

1 Answer 1

2

you should define your code this way:

function add_poll_answer_add() {
    poll_answer = poll_answer+1;
    jQuery('#poll_answers').append('<tr id="poll-answer-" Answer' + poll + '>
}
Sign up to request clarification or add additional context in comments.

3 Comments

is There a typo poll should be poll_answer?
yes I corrected the original question because it was a typo
thanks this works perfectly and is nice and simple to understand.

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.