0

In the code below i'm trying to run a function screw(); giving it the value of the span as a parameter to use

socket.on('usernames', function(data) {
  var html ='';
  for(i=0; i< data.length; i++){
    html += '<a><span onclick ="screw(data[i]);" style="font-weight: bold; cursor:pointer;">' + data[i] + '</span></a><br/>'
  }
  $('#online').html(html);
});

Please note that the "data" parameter being passed into the above function is an array automatically generated on the server side

here is the "screw" function in action. Im trying to at least alert the value it gets

  function screw(vem){
alert(vem);

}

But i keep getting that a reference error that data is undefined. Please can you spot what i'm doing wrong?'

4
  • Another one — stackoverflow.com/questions/16035617/… Commented Oct 9, 2016 at 9:42
  • can you show us how data looks like please? Commented Oct 9, 2016 at 9:44
  • @GeorgeKatsanos data is an array being updated when a user logs into the page with a username. All this is done on the server-side with the following code code var users = {}; socket.on('new-user', function(data, callback){ if (data in users){ callback(false); }else{ callback(true); socket.usernames = data; users[socket.usernames] = socket.id; update(); } }); function update(){ io.emit('usernames', Object.keys(users)); } code Commented Oct 9, 2016 at 9:53
  • @Rayon Thanks, i've seen my error. It was actually wrong string the syntax. in the onclick line Here's the right one : code html += "<a><span onclick ='screw(\""+data[i]+"\");' style='font-weight: bold; cursor:pointer;'>" + data[i] + "</span></a><br/>" code Commented Oct 9, 2016 at 10:08

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.