1

I have a code

 socket.on('user joined',function(data){
       var ume = data.username;
        $('#userlist').append("<li id='"+ume+"'><a href='#' onclick='prePrivate("+ume+"); return false;'>"+ume+"</a></li>");
        console.log(ume);
 });

and prePrivate method

  function prePrivate(userprivate){
        privateuser = userprivate;
        console.log(privateuser);
  }

When i click li element, console print

<li id="sample_data">

but not print sample_data. I do not understand, how to get variable. Sorry my english not good

4
  • 1
    what variable are you looking for? Commented Mar 31, 2015 at 2:54
  • i want get value of variable "ume" but code print <li id="sample_data"> Commented Mar 31, 2015 at 2:56
  • example: ume = "hoang" , i want console print hoang, but console print <li id="hoang"> Commented Mar 31, 2015 at 2:59
  • 1
    you need to add quotes around ume in the string where you call prePrivate onclick: ...onclick='prePrivate(\""+ume+"\"); ... Commented Mar 31, 2015 at 3:31

1 Answer 1

2

You need to add quotes around ume in the prePrivate call in the string that you are building: ...onclick='prePrivate(\""+ume+"\");...

Without them you are calling prePrivate(sample_data) and sample_data happens to be the DOM li element you just id'ed with that same name instead of a string. Why? See Do DOM tree elements with ids become global variables?

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

Comments

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.