1

i need in my code to repeat ten times this content, then i think that is possible in a loop like a for or while, but i tried and i can't

<li>
    <img style="float: left;" src="img/gaja.jpg" alt="Angry face" />
    <div style="width: 100px; height: 20px; float:left; margin-left: 5px; font: bold 12px arial;">IceMan</div>
    <div style="width: 150px; height: 20px; float:left; margin-left:5px;">Web Designer / FullTime</div>
</li>

any help?

thanks

3 Answers 3

5
var li = $('li');
for(var i = 0; i < 10; i++) {
    li.clone().appendTo(li.parent());
}
Sign up to request clarification or add additional context in comments.

6 Comments

why do u need jquery for something so trivial?
I think its jsut because jQuery is used for everything nowadays.. simple or not, jQuery makes it easy :)
Our whole life is a collection of trivial pieces. Feel free to use it that makes life easier
Is the answer really that simple? Is saving 3 lines always better than linking to several kilobytes of JavaScript library? (=another request) :)
the problem is not the library, i use the jquery in my project. the code is perfect. Thanks
|
2

Since the jQuery was not specified in the inital question I am going to assume it is not being used.

The pure JavaScript way would be like so:

var ul = document.getElementsByTagName("ul")[0];
var li = ul.getElementsByTagName("li")[0];
for(var i=1;i<10;i++){
    ul.appendChild(li.cloneNode(true));
}

Code example on jsfiddle.

Comments

0
for (var i = 0; i < 10; i++)
{
    document.write('<li>');
    document.write('<img style="float: left;" src="img/gaja.jpg" alt="Angry face" />');
    document.write('<div style="width: 100px; height: 20px; float:left; margin-left: 5px; font: bold 12px arial;">IceMan</div>');
    document.write('<div style="width: 150px; height: 20px; float:left; margin-left:5px;">Web Designer / FullTime</div>');
    document.write('</li>');
}

2 Comments

You can use really basic and pure JavaScript approach like this if you don't rely on JavaScript too much.
There are already posted better ways to do it. Mine was just the most basic one :)

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.