NeedHate NeedHate - 1 year ago 71
jQuery Question

How to get each of objects in list on click

I have list of questions.
And I have a html, f.e., . Each click, changes html ".title" with a next question from objects.
Click on button, I get: "Is it good to be me?",
next click, I get: "Who is the best man?" and so on...

var questions = {
first: "Is it good to be me?",
second: "Who is the best man?",
third: "Do you believe?",
fourth: "Are you sexy?"
var txt = "";
var x;

for(x in questions) {
txt += questions[x] + "<br>";

Answer Source

You can use array instead of object.

var questions = [
    "Is it good to be me?",
    "Who is the best man?",
    "Do you believe?",
    "Are you sexy?"

var index = 0;
<script src=""></script>
<button>Get question</button>

To getting index of question, use index variable. Also you can use data-index attribute to do this.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download