Zerobu
Zerobu

Reputation: 2691

Changing the attribute of a tag with jquery

I have this html code that i want to edit with jQuery. Here is the html.

<html> 
  <head> 
    <title>JQuery Problem 1</title> 
    <script type="text/javascript" src="jquery-1.4.min.js"></script> 
    <script type="text/javascript" src="problem1.js"></script> 
  </head> 
  <body> 
    <ol> 
      <li>Comp 250
        <ul> 
          <li>HTML</li> 
          <li>CSS</li> 
          <li>CGI</li> 
          <li>PHP</li> 
          <li>JavaScript</li> 
        </ul> 
      </li> 
      <li>Comp 345
        <ul> 
          <li>Objects and classes</li> 
          <li>Static and constant members</li> 
          <li>Operator overloading</li> 
          <li>Templates</li> 
          <li>Inheritance</li> 
          <li>Polymorphism</li> 
        </ul> 
      </li> 
      <li>Comp 431
        <ul> 
          <li>Perl language</li> 
          <li>File uploads and downloads</li> 
          <li>AJAX and JSON</li> 
          <li>Java Servlets</li> 
          <li>JSP</li> 
        </ul> 
      </li> 
    </ol> 
  </body> 
</html> 

What i want to do is to edit, the list tags of the Ol list. So the tags i want to edit is

<li>Comp 250
<li> Comp 345
<li> Comp 431

this is how i want them to be
 <li style="margin: 1em; font-weight: bold">Comp 250
  <li style="margin: 1em; font-weight: bold">Comp 245
    <li style="margin: 1em; font-weight: bold">Comp 431

Upvotes: 0

Views: 350

Answers (1)

cletus
cletus

Reputation: 625017

A few things:

  1. Use css() to set the style attribute;
  2. Because the <ul> is within the list element, the bold will apply to the child list elements as well.

So you have two options:

  1. Wrap the text in a <span>; or
  2. Set the font weight to bold and then the child list elements to font weight normal.

For the first one, getting the text node only is a little tricky but can be done. Try:

$("ol > li").each(function() {
  $($(this).contents()[0]).wrap("<span>").parent()
    .css({margin: "1em", fontWeight: "bold"});
});

The output is:

<ol>
  <li><span style="margin: 1em; font-weight: bold;">Comp 250 </span>
    <ul>
      <li>HTML</li>
      <li>CSS</li>
      <li>CGI</li>
      <li>PHP</li>
      <li>JavaScript</li>
    </ul>
  </li>
  <li><span style="margin: 1em; font-weight: bold;">Comp 345 </span>
    <ul>
      <li>Objects and classes</li>
      <li>Static and constant members</li>
      <li>Operator overloading</li>
      <li>Templates</li>
      <li>Inheritance</li>
      <li>Polymorphism</li>
    </ul>
  </li>
  <li><span style="margin: 1em; font-weight: bold;">Comp 431 </span>
    <ul>
      <li>Perl language</li>
      <li>File uploads and downloads</li>
      <li>AJAX and JSON</li>
      <li>Java Servlets</li>
      <li>JSP</li>
    </ul>
  </li>
</ol>

Alternatively you can change the font weight on the inner lists:

$("ol > li").css({margin: "1em", fontWeight: "bold"})
  .find("ul").css({fontWeight: "normal"});

Upvotes: 2

Related Questions