0

I want to add dynamically +1 to css selector each time function happens so it would select next question each time.

var questionCount = 2;
var previousQuestion = 1;

function start(){
document.getElementById("question" + questionCount).className = "question_box";
document.getElementById("question" + previousQuestion).className = "question_box disabled_gone";
$('#question' + previousQuestion).animate({height: '0', margin:'0',padding:'0'});
var questionCount = questionCount + 1;
var previousQuestion = previousQuestion + 1;
}

It works when I add first variables at the beginning of function but it will set variables to 2 and 1 once i run function again.

2
  • just do .className = "question_box" + questionCount ; Commented Nov 22, 2019 at 8:15
  • It will add +1 to class name. I need to add +1 do ID in css selector Commented Nov 22, 2019 at 8:17

1 Answer 1

1
var questionCount = questionCount + 1;
var previousQuestion = previousQuestion + 1;

Should be

questionCount++;
previousQuestion++;

// OR

questionCount = questionCount + 1;
previousQuestion = previousQuestion + 1;

I believe you are creating a new variable in the function scope, which is deleted once the function is over, meaning when it is run again it calls from the global scope, namely 2 & 1

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

2 Comments

Ah thanks, I still get confused about local and global variables sometimes.
Happens to all of us mate. Gl with this c:

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.