2

i have an array like:

var totalNum = new Array('img', 'img', 'img', 'img');

now on the documentready i added a div in the html page like:

$('#mainid').after('<div id="button"></div>');

the problem is i want to add <span></span> tags with total numbers in totalNum array like:

<div id="button">
   <span>1</span>
   <span>2</span>
   <span>3</span>
   <span>4</span>
</div>

i tried with for(){} loop into the .after() but got the syntax error. My code is here:

jQuery('#mainid').after('<div id="button">' + 
        for(i=1;i<=totalNum.length;i++){
    jQuery('<span>'+i+'</span>').appendTo('#button');
}
    + '</div>');
4
  • 1
    #button and #jp-button aren't the same divs Commented Feb 29, 2012 at 7:12
  • @SeanCarruthers sorry now changed, it was a copy paste by me. Commented Feb 29, 2012 at 7:13
  • Alright, was just making sure that wasn't your problem :) Commented Feb 29, 2012 at 7:13
  • @SeanCarruthers Thanks for your quick reply. yes i am sure that was not my problem. Commented Feb 29, 2012 at 7:14

5 Answers 5

2

You can't throw a for loop in the middle of string concatenation.

var button = $('#button');

$.each(totalNum, function(index) {
    $('<span />', { text: index + 1 }).appendTo(button);
});

I believe this has some benefits such as...

  • Not forcing the browser to parse HTML.
  • Caching a reference to the target element #button.
  • $.each() to me is more elegant than an explicit for loop.
Sign up to request clarification or add additional context in comments.

Comments

1
jQuery('#mainid').after('<div id="button"></div>');

for(i=1;i<=totalNum.length;i++){
    jQuery('<span>'+i+'</span>').appendTo('#button');
}

OR

var tmpStr = '';
for(i=1;i<=totalNum.length;i++){
    tmpStr += '<span>'+i+'</span>';
}

jQuery('#button').append(tmpStr);

This would be a better approach to prevent the syntax error & append.

Comments

1

You can't concatenate a for loop like that! Also you forgot to declare i. Then is recomended to cache the length and append all html strings in a variable and append to object at last to avoid stressing the DOM as much as possible. You can do this:

var spans = ''; 
for (var i=1, len=totalNum.length; i<len; i++) {
    spans += '<span>'+ i +'</span>';
}
$('<div id="button">').append(spans).appendTo('#mainid');

Comments

1

Use the map function to loop the array as an expression, and use join to concatenate the array items into a single string:

jQuery('#mainid').after(
  '<div id="button">' + 
  jQuery.map(totalNum, function(e,i){ return '<span>'+(i+1)+'</span>'; }).join('') +
  '</div>'
);

Demo: http://jsfiddle.net/Guffa/U48yP/

1 Comment

wow, i didn't knew about this. but 'linuxeasy's answer also worked for me. But thanks a lot to aware about the .map() +1 ;)
0

Try just:

jQuery('#mainid').after('<div id="button"></div>');

for( i=1; i<=totalNum.length; i++ ) {
   jQuery('#button').append('<span>' + i + '</span>');
}

I switched from .appendTo to .append, because that just seemed like an indirect way of doing things. This is a little more clear as to what we're doing.

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.