[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 292
  • Last Modified:

jQuery: Insert 30 empty span tags

Using jQuery, what is the best way to insert 30 empty span tags into a paragraph?  My code below works but I want to do it with less code.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Demo</title>
<style type="text/css">
span {
display: block;
border: 1px solid red; margin: 5px; float: left; height: 5px; width: 5px; background: blue;
}
</style>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
<script type="text/javascript">

$(document).ready(function() {
 $('p').append('<span />')
 $('p').append('<span />')
 $('p').append('<span />')
 $('p').append('<span />')
 $('p').append('<span />')
 $('p').append('<span />')
 $('p').append('<span />')
 $('p').append('<span />')
 $('p').append('<span />')
 $('p').append('<span />')
 $('p').append('<span />')
 $('p').append('<span />')
 $('p').append('<span />')
 $('p').append('<span />')
 $('p').append('<span />')
 $('p').append('<span />')
 $('p').append('<span />')
 $('p').append('<span />')
 $('p').append('<span />')
 $('p').append('<span />')
 $('p').append('<span />')
 $('p').append('<span />')
 $('p').append('<span />')
 $('p').append('<span />')
 $('p').append('<span />')
 $('p').append('<span />')
 $('p').append('<span />')
 $('p').append('<span />')
 $('p').append('<span />')
 $('p').append('<span />')
});

</script>
</head>
<body>

<p></p>

</body>
</html>

Open in new window

0
hankknight
Asked:
hankknight
3 Solutions
 
leakim971PluritechnicianCommented:
$(document).ready(function() {
 for(var i=0;i<30;i++) $('p').append('<span />');
});
0
 
hankknightAuthor Commented:
How could I do that inline like this:

$('div').after( '<p class="slidesNav">'+  _30_SPAN_TAGS_HERE '+'</p>');

Open in new window

0
 
abolinhasCommented:
Try this
<script>
$(document).ready(function() {

 for(var i=0;i<30;i++) $('#test').add('<p><span></span></p>').appendTo(document.body);

});
</script>
<div id="test"></div>

Open in new window

0
 
mcnuteCommented:
I would use a combination of the two solutions:

$(document).ready(function() {
    $('div').after( '<p class="slidesNav"></p>');

     var spans;
     for(var i=0;i<30;i++) {
          spans += '<span></span>';
     }

$('.slidesNav').append(spans);
}); 
                                            

Open in new window


This is more performant than using the append method 30 times. By doing so you would use the append method only once with a text variable which contains 30 span tags.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now