Reset increments for each list
Greetings,
So i have this code which is doing what i sorta want but not really. Right now it just looks for all li elements of a ordered list inside a div with id content. it prepends a span tag with a number that increments from 1 yadda yadda. Ok, now the problem. When there are additional ordered lists, it should reset, right now it just continues off where the last list ends. See example below:
JS:
$('#content ol li').each(function(j){
var bulletNum = j+1;
$(this).prepend('<span class="bullet-'+bulletNum+'"></span>');
});
Outputted html:
<div id="content">
<ol>
<li><span class="bullet-1">what am</span</li>
<li><span class="bullet-2">i doing</span</li>
<li><span class="bullet-3">wrong?</span</li>
</ol>
<ol>
<li><span class="bullet-4">what am</span</li>
<li><span class="bullet-5">i doing</span</li>
<li><span class="bullet-6">wrong?</span</li>
</ol>
</div>
What should happen:
<div id="content">
<ol>
<li><span class="bullet-1">what am</span</li>
<li><span class="bullet-2">i doing</span</li>
<li><span class="bullet-3">wrong?</span</li>
</ol>
<ol>
<li><span class="bullet-1">what am</span</li>
<li><span class="bullet-2">i doing</span</li>
<li><span class="bullet-3">wrong?</span</li>
</ol>
</div>
I know this is probably easy but what am I doing wrong here?
Strange code, what language is that?
Here's all I could decipher, the code you may be looking for is at end
HTML Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD> <TITLE> Th5-59pm50</TITLE>
<META http-equiv=Content-Type content="text/html; charset=UTF-8" >
<STYLE type=text/CSS>
* {margin:20; padding:0;}
</STYLE>
<META content="MSHTML 6.00.2900.2963" name=GENERATOR> </HEAD>
<BODY scroll="auto" >
JS:
$('#content ol li').each(function(j){
var bulletNum = j+1;
$(this).prepend('<span class="bullet-'+bulletNum+'" > </span> ');
});
Outputted html:
<div id="xcontent" >
<ol>
<li> <span class="bullet-1" > what am</span</li>
<li> <span class="bullet-2" > i doing</span</li>
<li> <span class="bullet-3" > wrong?</span</li>
</ol>
<ol>
<li> <span class="bullet-4" > what am</span</li>
<li> <span class="bullet-5" > i doing</span</li>
<li> <span class="bullet-6" > wrong?</span</li>
</ol>
</div>
What should happen:
<div id="content" >
<ol>
<li> <span class="bullet-1" > what am</span</li>
<li> <span class="bullet-2" > i doing</span</li>
<li> <span class="bullet-3" > wrong?</span</li>
</ol>
<ol>
<li> <span class="bullet-1" > what am</span</li>
<li> <span class="bullet-2" > i doing</span</li>
<li> <span class="bullet-3" > wrong?</span</li>
</ol>
</div>
<SCRIPT type="text/javascript" >
var co=document.getElementById('content').getElementsByTagName('ol');
for(i=0;i<co.length;i++){ var l=co[i].getElementsByTagName('li');
for(j=0;j<l.length;j++){l[j].innerHTML='<span class="bullet-'+(j+1)+'" title="class=bullet-'+(j+1)+'" >'+(j+1)+'</span>'}
}
</SCRIPT>
</BODY> </HTML>
thanks. im going to give that a try. I'm using jquery btw I should of stated that.
Thanks Justin! That worked perfectly. Can you tell me what I needed to do?
Its only javascript, hope you see whats going on and can work with it.
one things though. innerHTML is replacing all the content inside the li element. I just want the span tags to be prepended to the li element.
<li><span class="bullet-1"></span> lorem ipsum dolor sit</li>
and not
<li><span class="bullet-1"></span></li>
HTML Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD> <TITLE> Th5-59pm50</TITLE>
<META http-equiv=Content-Type content="text/html; charset=UTF-8" >
<STYLE type=text/CSS>
* {margin:20; padding:0;}
</STYLE>
<META content="MSHTML 6.00.2900.2963" name=GENERATOR> </HEAD>
<BODY scroll="auto" >
<div id="content" >
<ol>
<li> how</li>
<li> about</li>
<li> this</li>
</ol>
<ol>
<li> then?</li>
<li> yes</li>
<li> no?</li>
</ol>
<ol>
<li> I knew</li>
<li> you'd be back</li>
<li> for this</li>
</ol>
</div>
<SCRIPT type="text/javascript" >
var co=document.getElementById('content').getElementsByTagName('ol');
for(i=0;i<co.length;i++){ var l=co[i].getElementsByTagName('li');
for(j=0;j<l.length;j++){ var ih=l[j].innerHTML;
l[j].innerHTML='<span class="bullet-'+(j+1)+'" title="class=bullet-'+(j+1)+'" >'+ih+'</span>'}
}
</SCRIPT>
</BODY> </HTML>
Here's another way if you already have spans inside the <li>
HTML Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD> <TITLE> Th5-59pm50</TITLE>
<META http-equiv=Content-Type content="text/html; charset=UTF-8" >
<STYLE type=text/CSS>
* {margin:20; padding:0;}
.bullet-1{color:green}
.bullet-2{color:red}
.bullet-3{color:blue}
.bullet-4{color:#FF9900}
</STYLE>
<META content="MSHTML 6.00.2900.2963" name=GENERATOR> </HEAD>
<BODY scroll="auto" >
<div id="content" >
<ol>
<li> <span> how</span> </li>
<li> <span> about</span> </li>
<li> <span> this</span> </li>
</ol>
<ol>
<li> <span> then?</span> </li>
<li> <span> yes</span> </li>
<li> <span> no?</span> </li>
</ol>
<ol>
<li> <span> I knew</span> </li>
<li> <span class="bullet-4" > you'd be</span> </li>
<li> <span> back</span> </li>
<li> <span> for this</span> </li>
</ol>
</div>
<SCRIPT type="text/javascript" >
var co=document.getElementById('content').getElementsByTagName('ol');
for(i=0;i<co.length;i++){ var l=co[i].getElementsByTagName('li');
for(j=0;j<l.length;j++){ l[j].getElementsByTagName('span')[0].className='bullet-'+(j+1)+'';
}
}
</SCRIPT>
</BODY> </HTML>
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Bookmarks