Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery/Javascript: What's wrong with my 'If'?

I have a little hunk of my code here:

if(".level-0.find('.children').length == 1"){
     $(".level-0 > a").attr("href", ""); 
 };

Basically I'm saying "If Level 0 has a class of '.children', don't rewrite the HREF in it's link!"

The problem is that it ALWAYS overwrites the HREF, as if there were no conditional. I've changed that "== 1" to "== 10000000" or "20" or "15" and it ALWAYS overwrites the HREF. It's just acting as if there's no conditional statement. I'm sure this is a synatx goof, but I can't see it.

Again, thanks for helping a n00b stumble his way towards minimal competency. Here's the code we're looking to change, just for context.

<li class="level-0 page_item page-item-264"><a href="http://www.domain.com.php5-15.dfw1-1.websitetestlink.com/wordpress/?page_id=264" title="Ca$h Rulez">Ca$h Rulez</a>
<ul class='children'>
    <li class="level-1 page_item page-item-266"><a href="http://www.domain.com.php5-15.dfw1-1.websitetestlink.com/wordpress/?page_id=266" title="1994">1994</a></li>
    <li class="level-1 page_item page-item-268"><a href="http://www.domain.com.php5-15.dfw1-1.websitetestlink.com/wordpress/?page_id=268" title="1995">1995</a></li>
    <li class="level-1 page_item page-item-270"><a href="http://www.domain.com.php5-15.dfw1-1.websitetestlink.com/wordpress/?page_id=270" title="1996">1996</a></li>
    <li class="level-1 page_item page-item-272"><a href="http://www.domain.com.php5-15.dfw1-1.websitetestlink.com/wordpress/?page_id=272" title="1997">1997</a></li>
    <li class="level-1 page_item page-item-274"><a href="http://www.domain.com.php5-15.dfw1-1.websitetestlink.com/wordpress/?page_id=274" title="1998">1998</a></li>
    <li class="level-1 page_item page-item-276"><a href="http://www.domain.com.php5-15.dfw1-1.websitetestlink.com/wordpress/?page_id=276" title="1999">1999</a></li>
    <li class="level-1 page_item page-item-278"><a href="http://www.domain.com.php5-15.dfw1-1.websitetestlink.com/wordpress/?page_id=278" title="2000">2000</a></li>
    <li class="level-1 page_item page-item-280"><a href="http://www.domain.com.php5-15.dfw1-1.websitetestlink.com/wordpress/?page_id=280" title="2001">2001</a></li>
</ul>
</li>
like image 329
GilloD Avatar asked Dec 06 '25 20:12

GilloD


1 Answers

Shouldn't it rather be

if ($(".level-0").find('.children').length == 1) {
    $(".level-0 > a").attr("href", ""); 
}

?

Late friday maybe? :)

like image 141
BalusC Avatar answered Dec 08 '25 12:12

BalusC



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!