PNS
PNS

Reputation: 770

css display property changed via javascript

I'm having a little problem with the css display property.

I have the following pieces of code:

<input type="text" name="extra" value="" id="i1" style="display:block">

and

<script type="text/javascript">
function changeVisibility(el){
            if(el.style.display="block"){el.style.display="none";}
            else { el.style.display="block";}                 
        }
</script>

problem is it doesn't work :) it starts out being displayed as a block and changing to none, but whenever I try calling the method again it doesn't change back to block?

Also when I try to view the page source when the field is no longer visible it is still set to "block" ?

Any ideas how I can fix this?

Upvotes: 0

Views: 147

Answers (2)

Dan Appleyard
Dan Appleyard

Reputation: 7445

change your if statement to use == and not =. That may be your problem. Your if statement is always coming back as true, so it is always setting the display to none.

Upvotes: 1

meder omuraliev
meder omuraliev

Reputation: 186652

the equality operator is ==, not =.

You are forever assigning it by style.display="block" so add another = to change the operation from assignment to equality.

Upvotes: 3

Related Questions