Justin Erswell
Justin Erswell

Reputation: 708

Simple jQuery Show / Hide

I have an input which is hidden which carrys a value based on that value I would like to show a menu item.

i.e. if value in field = 1 than show menu item 1 else if value in field is not 1 then hide menu item 1

my jquery

$(function() {
    $("#sessionVal".val(==1){
        $("#adminMenu").show();
    });
    $("#sessionVal".val(!=1){
        $("#adminMenu").hide();
    });
});

Really simple I am sure for most can anyone help?

Upvotes: 0

Views: 264

Answers (4)

anthony sottile
anthony sottile

Reputation: 69854

if($('#sessionVal').val() == 1) {
    $('#adminMenu').show();
} else {
    $('#adminMenu').hide();
}

Should do the trick.

Note I am using == instead of === because I'm assuming the value attribute won't necessarily get returned as number

Upvotes: 1

Rihards
Rihards

Reputation: 10349

$(document).ready(function() {
    if( $("#sessionVal").val() == 1) {
        $("#adminMenu").show();
    } else {
        $("#adminMenu").hide();
    };
});

Not sure when do you want it to run, in this example it will check the field once page is loaded, you can bind however click, focus or other events when it should be done..

Upvotes: 2

josemota
josemota

Reputation: 984

Justin, instead of .val(==1), try .val()=='1', and the same for the other statement. I believe that would throw you an error, am I right?

Hope to have helped.

Upvotes: 0

kei
kei

Reputation: 20471

You got your syntax wrong

if ( $("#sessionVal").val() == 1 )

Upvotes: 1

Related Questions