fix a jquery if stament

Hello this should be an easy one.. I need to fix this..



function menuControll(url){
//alert('checking the menu- went to: '+url); out puts the right url....
$("#menuwrapper a,#menuwrapper li").removeClass('menuactive');
if(url==$("#menuwrapper a").each().attr('href')){
$("#menuwrapper a,#menuwrapper li").addClass('menuactive');
alert('checking the menu'); not getting called...
}
}

smaple menu is attacted

I do get the first alert and I am getting the right url.. but.. I can't seem to get the if stament..

Thank you -j
<!-- the html for the menu -->


<div  class="corner30pxRight" id="menuwrapper">
<ul id="primary-nav">
	<li class=""><a  accesskey="1" href="http://domain.com/" class="first" ><span>Home</span></a></li>
	<li class="menuparent"><a  href="http://domain.com/outfitter-guide-service.html" class="menuparent"><span>Outfitter / Guide Service</span></a>
        <ul>
            <li><a href="http://domain.com/outfitter-guide-service/what-to-bring.html" class=""><span>What to Bring</span></a></li>
            <li class="menuparent"><a href="http://domain.com/outfitter-guide-service/locations.html" class="menuparent"><span>Locations</span></a>
                <ul>
                    <li><a href="http://domain.com/outfitter-guide-service/locations/snake-river-boat-launches.html" class=""><span>Boat Launches</span></a></li>
                </ul>
            </li>
            <li><a href="http://domain.com/outfitter-guide-service/the-lodge.html" class=""><span>Lodging</span></a></li>
            <li><a href="http://domain.com/outfitter-guide-service/rates.html" class=""><span>Rates</span></a></li>
            <li><a href="http://domain.com/outfitter-guide-service/our-boats.html" class=""><span>Our Boats</span></a></li>
            <li><a href="http://domain.com/outfitter-guide-service/our-guides.html" class=""><span>Our Guides</span></a></li>
            <li><a href="http://domain.com/outfitter-guide-service/testimonials.html" class=""><span>Testimonials</span></a></li>
        </ul>
	</li>
	<li><a href="http://domain.com/contact.html" class=""><span>Contact Us</span></a></li>
	<li class="menuparent"><a href="http://domain.com/albums.html" class="menuparent"><span>Albums / Videos</span></a>
        <ul>
            <li><a href="http://domain.com/albums/Groups.html" class=""><span>Groups</span></a></li>
            <li><a href="http://domain.com/albums/boat-pictures.html" class=""><span>Boat Pictures</span></a></li>
            <li><a href="http://domain.com/albums/chukar-hunts---cast-blast.html" class=""><span>Chukar Hunts - Cast &amp; Blast</span></a></li>
            <li><a href="http://domain.com/albums/bass.html" class=""><span>Bass</span></a></li>
            <li><a href="http://domain.com/albums/fall-chinook.html" class=""><span>Fall Chinook</span></a></li>
            <li><a  href="http://domain.com/albums/spring-chinook.html" class=""><span>Spring Chinook</span></a></li>
            <li><a href="http://domain.com/albums/steelhead.html" class=""><span>Steelhead</span></a></li>
            <li><a href="http://domain.com/albums/sturgeon.html" class=""><span>Sturgeon</span></a></li>
            <li><a href="http://domain.com/albums/walleye.html" class=""><span>Walleye</span></a></li>
            <li><a href="http://domain.com/albums/wildlife.html" class=""><span>Wildlife</span></a></li>
            <li><a href="http://domain.com/albums/videos.html" class=""><span>Videos</span></a></li>
        </ul>
	</li>
	<li class="menuparent"><a  href="http://domain.com/Blogs.html" class="menuparent last" ><span>Fishing Reports</span></a>
        <ul>
            <li><a href="http://domain.com/Blogs/fish-counts-and-cam.html" class=""><span>Fish Counts and Cam</span></a></li>
        </ul>
    </li>
</ul>
<div class="clearb"></div>
</div>

Open in new window

LVL 4
jeremyBass26Asked:
Who is Participating?
 
jimmym715Commented:
The problem is actually that you were mis-using the .each() functionality

The code below should do the trick for you.
function menuControll(url)
    {
        $("#menuwrapper a,#menuwrapper li").removeClass('menuactive');
        $("#menuwrapper a").each(function(i){
            if(url == $(this).attr('href'))
            {
                $("#menuwrapper a,#menuwrapper li").addClass('menuactive');
            }
        });
    }

Open in new window

0
 
jeremyBass26Author Commented:
yeah I saw that... I was just trying out the code below.. but you just jogged my head.. your right I needed the if on the inner func of the each() .. tk .. -J
function menuControll(url){
    //alert('checking the menu- went to: '+url); out puts the right url.... 
    $("#menuwrapper a,#menuwrapper li").removeClass('menuactive');
    if(url==$("#menuwrapper a").each(function(){$(this).attr('href');})){
        $("#menuwrapper a,#menuwrapper li").addClass('menuactive');
        alert('checking the menu'); 
    }
}

Open in new window

0
 
jeremyBass26Author Commented:
tk for the brain jog lol.. cheers
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
jeremyBass26Author Commented:
wait.. shoot.. that was not right.. it's applying to all of them..


function menuControll(url)
    {
        $("#menuwrapper a,#menuwrapper li").removeClass('menuactive');
        $("#menuwrapper a").each(function(i){
            if(url == $(this).attr('href'))
            {
                $("#menuwrapper a,#menuwrapper li").addClass('menuactive');
            }
        });
    }


$("#menuwrapper a,#menuwrapper li") get it.. hmm...
0
 
jeremyBass26Author Commented:
ok.. it's
function menuControll(url)
    {
        $("#menuwrapper a,#menuwrapper li").removeClass('menuactive');
        $("#menuwrapper a").each(function(i){
            if(url == $(this).attr('href')){
                $(this).addClass('menuactive').closest('li').addClass('menuactive');
            }
        });
    }

Open in new window

0
 
jimmym715Commented:
ah yes...I didn't have the CSS to catch what you noticed, but you're right

try this:

function menuControll(url)
    {
        $("#menuwrapper a,#menuwrapper li").removeClass('menuactive');
        $("#menuwrapper a").each(function(i){
            if(url == $(this).attr('href'))
            {
                $(this).addClass('menuactive');
            }
        });
    }

once you're in the if statement, you've already got a handle on the element you want, so $(this) should be all you need!

0
 
jeremyBass26Author Commented:
umm.. no no.. sorry .. I know for sure you can use just this as this is in reference to the a only due to the href arg... so if anyone is interested...

this is what it's for... http://www.hellscanyonsportfishing.com/

here is the code as of yet..
    function menuControll(url){
        $("#menuwrapper a").each(function(i){
            if(url == $(this).attr('href')){
                $("#menuwrapper a,#menuwrapper li").removeClass('menuactive childactive');
                $(this).addClass('menuactive')
                    .closest('li').addClass('menuactive')
                    .closest('li.menuparent').addClass('menuactive childactive')
                    .closest('a.menuparent').addClass('menuactive childactive');
            }
        });
    }



this takes care of two deep but I need to go 3 deep as I just realized :/  


in case i have a related question to this site in case anyone wants to give the back button issues a wack at fixing..

http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Jquery/Q_25974975.html

tk-J
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.