0

I'm creating a list with jquery

$('<li value="0" onclick="'if(cond){ }'"> </li>).appendTo('#ul list');

The problem is in the section onclick, can someone tell me the right syntax?

4
  • what exactly are you trying to achieve? and li has not a value attribute. Commented Oct 2, 2012 at 12:33
  • Next time, check the browser's error console first. Your code throws. Commented Oct 2, 2012 at 12:36
  • Btw, LI elements don't have a value attribute, i.e. the HTML standard doesn't define such an attribute for LI elements. Commented Oct 2, 2012 at 12:37
  • problem solved thanks to @xdazz, and li do have value! Commented Oct 2, 2012 at 12:47

1 Answer 1

2

Since you use jQuery, then don't use the onclick attribute.

$('<li>').attr('value', '0').click(function() {
  if (cond) {
    //...
  }
}).appendTo('#ul list');
Sign up to request clarification or add additional context in comments.

3 Comments

This would achieve what I suppose the user was asking. But the expression "#ul list" for the selector seems strange to me.
@emas You may be right, but that is what the OP is using. I am also not sure what what list references. I think he wants $('#list') and is confused about the # symbol. I think he wants the UL named LIST.
@user1638466 mark the answer as answered (hover over the number to its left and click the tick)!

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.