Jump to content

jquery ul li ul html()


es131245

Recommended Posts

<ul><li>1  <ul>   <li>11</li>   <li>12</li>   <li>13</li>  </ul></li><li>2  <ul>   <li>21	<ul>	 <li>212</li>	</ul>   </li>   <li>22</li>  </ul></li><li>3</li></ul>

$(ul li).each(function(){$(this).html('_'+$(this).html());});

works only for first levelmoreover

$(ul li,ul li ul li,ul li ul li ul li).each({$(this).html('_'+$(this).html());});

isnt working ether What is wrong?Why selectors work diferent way than css? PS

$(ul li).each(function(){$(this).css({'background':'url("favicon.ico") top left no-repeat'});});

Works ( blame html\(\)?) :Pleased:

Edited by es131245
Link to comment
Share on other sites

sorry error while typing topic. Topic isnt solved yet.

<html><head><script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script></head><body><ul><li>1  <ul>   <li>11</li>   <li>12</li>   <li>13</li>  </ul></li><li>2  <ul>   <li>21	    <ul>		 <li>212</li>	    </ul>   </li>   <li>22</li>  </ul></li><li>3</li></ul><script type="text/javascript">$(document).ready(function(){$('ul li').each(function(){$(this).html('_'+$(this).html());});});</script></body></html>

Link to comment
Share on other sites

The problem is, you are not just targeting <li>11</li> but you will be targeting all the sub level list items as well, within <li> 1 <ul> <li>11</li> <li>12</li> <li>13</li> </ul></li> and end up overwriting them each time, the only sure thing to target is the <li> tag itself, and replace with '<li>_'.

$('ul').html($('ul').html().replace(/<li>/gi,'<li>_'));

As usual had to add amendment to cover crappy IE, as in i for case insensitive

Edited by dsonesuk
Link to comment
Share on other sites

replace doent help because i use $(this).attr('id') Prepend helped

$('ul#index li').each(function(){if(typeof($(this).attr('id'))!='undefined')  {$(this).prepend('<span class="number">'+$(this).attr('id').substr(4)+'</span>');}

SOLVED thank you :)

Edited by es131245
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...