0

I am building a list automatically from a JSON with the following code

dataObject.forEach(newList);

function newList(item, index) {
  var s = item.time;
  alert(s);
  var list = $('<li onclick="myFunction(s)">' + listItemString + '</li>');
....
}

The alert(s) print the right values of s but when I click on the element I get an error ReferenceError: s is not defined

3 Answers 3

1

You have to append the variable s

function newList(item, index) {
  var s = item.time;
  var list = $('<li onclick="myFunction(\'' + s + '\')">' + listItemString + '</li>');
  ....
}
Sign up to request clarification or add additional context in comments.

Comments

1

You need to use

var list = $('<li>' + listItemString + '</li>');
list.on('click', function {
    myFunction(s);
})

Comments

0

Try with:

var list = $('<li onclick="myFunction(this)">' + listItemString + '</li>');

Then you can access item.time inside myFunction

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.