jquery selection condition


<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
<div id="myparent">
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
<li>7</li>
</ul>
</div>

<div id=”another_thing”>
<ol>
<li>one</li>
<li>two</li>
</ol>
</div>

</body>
</html>

jQuery(
   function ($) {
      $("li").filter("[class*=c]").css("color", "red");
      $("li").filter(
         function (i) {return i!=5;}
       ).css("color", "red");
    }
);
jQuery(
   function ($) {
       jq = $("ul");
       $("div").has(jq).css("color", "red");// seems like a more elegant selecting method.
    }
);

// this will return 9
jQuery(
    function ($) {
         l = $("div").find("li").length;
         console.log(l);
     }
);

function ($) {
     $("li").closest("[class=parent]").css("list-style", "none");// this will remove the list style
      $("li").parentsUntil("[id=myparent]").css("margin-top", "50px");// this will put margin on both divs
   }
);
jQuery(
  function ($) {
    $("li").closest("[class=parent]").css("list-style", "none");
    $("li").parentsUntil("[id=myparent]").css("margin-top", "50px");
    $("li").next("[class=c]").css("color", "blue");
    $("li[class=d]").nextUntil("[class=f]").css("color", "silver");
  }
);
the call to prepend has the effect of moving the elements rather than adding them twice. 
To address this issue, you need to create copies of the elements you want to insert by 
using the clone method. Listing 7-8 shows the revised script. 
Listing 7-8. Cloning Elements So They Can Be Added to the Document More Than Once 
<script type="text/javascript"> 
    $(document).ready(function() {
          var orchidElems = $("<div class='dcell'/>")
                            .append("<img src='orchid.png'/>") 
                            .append("<label for='orchid'>Orchid:</label>")
                            .append("<input name='orchid' value='0' required />"); 
           var newElems = $("<div class='dcell'/>") 
                            .append("<img src='lily.png'/>") 
                            .append("<label for='lily'>Lily:</label>") 
                            .append("<input name='lily' value='0' required />")
                            .add(orchidElems); 
           newElems.css("border", "thick solid red");
           $('#row1').append(newElems); 
           $('#row2').prepend(newElems.clone()); 
      }); 
</script>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s