0

I've written a JS program which works fine if I use the following line of code:

    li = $("<li data-role='collapsible' data-iconpos='right' data-inset='false'></li>");

But if I change the above line to as follows, it stops working:

    li = $('<li/>, {
          data-role: 'collapsible'
          data-iconpos: 'right'
          data-shadow: 'false'
          data-corners: 'false'
    });

What is wrong here?

EDIT - Additional code that is failing:

    a = $('<a/>', {
        href: fSelectedContent[i].hPageURL,
        click: function () { mSelectCount(fSelectedContent[i].rContentID);},
        html: fSelectedContent[i].rContentName
    });
4
  • 1
    The first one is valid html while the second is not. It looks like you're missing an end quote after <li/>. Commented Aug 29, 2014 at 19:51
  • You're missing a single quote after the first argument to $: $('<li />') Commented Aug 29, 2014 at 19:52
  • You accidentally a double quote. Commented Aug 29, 2014 at 19:52
  • Thanks folks. It worked like a charm. But my attempt to convert another line failed. Please see above. Commented Aug 29, 2014 at 20:37

3 Answers 3

6

In a JavaScript object literal, a property name must be an identifier or a string.

An identifier cannot include a - character, so you have to use strings.

Quote your property names.


You also need a comma between each key:value pair.


You also need to put a quote to end the string for the <li/>.


li = $('<li/>', {
      "data-role": 'collapsible',
      "data-iconpos": 'right',
      "data-shadow": 'false',
      "data-corners": 'false'
});
Sign up to request clarification or add additional context in comments.

3 Comments

and do not quote booleans.
@rvignacio — They are data attribute values, so they are strings, not booleans.
@Quentin, the value false in JS is obviously a boolean, it isn't necessary to quote it, jQuery's .attr() method will put the quotes when adding it to the DOM, and more important, it will put the correct value for attributes like required (e.g.: try $('<div>', {required: false})).
2

An alternative would have been to use:

$('<li/>').attr({
    "data-role": 'collapsible',
    "data-iconpos": 'right',
    "data-shadow": false,
    "data-corners": false
});

But each to their own I guess :)

5 Comments

What do you mean by "consistent jQuery"?
I mean that .attr() function is provided as part of jQuery, so why not make use of it?
jQuery also provides $(html, attribtes), why not make use of it? That being said, I also prefer the attr form and such is not deserving of a downvote. But, take care not to implicitly deny the legality of the initial form and point out (in words) the problems with the original so that they can be easily recognized.
Because $('<li/>, {...}) is also provided as part of jQuery and is smaller?
Like I say, each to their own
0

For string literals, if you want to have them roll to the next line you need to use a backslash at the end of the line.

var something = 'line 1 \
    line2';

In your case though, you're just missing an end-quote after <li/>.

$('<li/>', {

(And, do note Quentin's suggestion above of having proper object literal keys. You need to quote them if you have - in there, or other reserved characters. +1 Quentin.)

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.