tags:

views:

135

answers:

4

Is this the correct format?

var title = new_element.setAttribute('jcarouselindex', "'items+1'");
alert(title);
+2  A: 

No need to put the second parameter in quotes.

var title = new_element.setAttribute('jcarouselindex', items+1);

If you want to set a custom attribute then you can use HTML5 data-attributes, something like

var title = new_element.setAttribute('data-jcarouselindex', items+1);
rahul
A: 

It is syntactically correct JS/DOM, but there is no 'jcarouselindex' attribute in HTML and using setAttribute (as opposed to setting properties that map to attributes) is generally bad practice as it is buggy in MSIE (although not in such a way that will cause a problem in this case).

You might not be intending to have <foo jcarouselindex="&#39;items+1&#39;"> as your end result though.

David Dorward
A: 

Using jQuery?

var title = $('#new_element').attr('jcarouselindex', "'items+1'"); alert(title);

Tim
You're showing how to set the attribute, but also get it. jQuery is good to get over cross-browser compatibility issues, but if you're already using it, you might as well use `.data`.
Kobi
A: 

You have too many quotes:

var index = items + 1;
new_element.setAttribute('jcarouselindex', index);

Remark: there's no jcarouselindex attribute defined in HTML elements so this is not very clean code.

Darin Dimitrov