tags:

views:

34

answers:

3
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
<html><head>
<meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>
<title>If Else</title>
<script type="text/javascript" src="/jquery-1.3.2.min.js"></script>
<style  TYPE="text/css"> 

    .show                                       { display:block; }
    .hide                                       { display:none; }

</style>
</head>
<body>
<script type="text/javascript">
$('#portweb').click(function() {

    if ('li[value*=web] img'.hasClass("hide")) {
        $('li[value*=web] img').removeClass('hide').addClass('show');
    }
    else if ('li[value*=web] img'.hasClass("show")) {
        $('li[value*=web] img').removeClass('show').addClass('hide');
    }

});
</script>

<a href="#" id="portweb">Web</a>
<br />

        <ul id="portfolioWrapperSet">
            <li value="web dev"><a href="#"><img src="images/portfilio/ph1.jpg" class="hide" /></a></li>
            <li value="web packaging print media"><a href="#"><img src="images/portfilio/ph2.jpg" class="hide" /></a></li>
            <li value="tv packaging print media"><a href="#"><img src="images/portfilio/ph3.jpg" class="hide" /></a></li>
            <li value="web packaging print media social"><a href="#"><img src="images/portfilio/ph4.jpg" class="hide" /></a></li>
        </ul>


</body>
</html>

So the problem im having is actually getting my function to remove the '.hide' class then add the '.show' class through my if statement.

+1  A: 

You ought to use the .show() and .hide() methods really as what you're trying to do is what they were designed for.

You need to wrap your code into a function that will get executed when the ready event is raised

$(function() { /* code here */ });

So it should be like so

$(function() {

    $('#portweb').click(function() {

        if ('li[value*=web] img'.hasClass("hide")) {
            $('li[value*=web] img').removeClass('hide').addClass('show');
        }
        else if ('li[value*=web] img'.hasClass("show")) {
            $('li[value*=web] img').removeClass('show').addClass('hide');
        }

    });

});
Russ Cam
Thanks for the response Russ!
Starboy
No probs. I would cache the jQuery object in a variable too so that you don't search the Dom each time to get the same elements
Russ Cam
A: 

You are trying to manipulate the DOM before it is ready. Try wrapping your code in a document.ready statement. Also in your ifs you are trying to call the hasClass method on a string which is not defined:

$(function() {

    $('#portweb').click(function() {

        if ($('li[value*=web] img').hasClass("hide")) {
            $('li[value*=web] img').removeClass('hide').addClass('show');
        }
        else if ($('li[value*=web] img').hasClass("show")) {
            $('li[value*=web] img').removeClass('show').addClass('hide');
        }

    });

});
Darin Dimitrov
Thanks Darin, this did work. Now I had this at one time in a $(document).ready(function(){ }); and still had no success, but thank you for your response. (this brought up another problem actually)
Starboy
A: 

Try changing your if conditional to something like this:

if ($('li[value*=web] img').hasClass("hide")) {
        $('li[value*=web] img').removeClass('hide').addClass('show');
    }
    else if ($('li[value*=web] img').hasClass("show")) {
        $('li[value*=web] img').removeClass('show').addClass('hide');
    }
Keith Rousseau