Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot remove a class hidden by JavaScript

I would like hide a div when it is loaded and show it when a button is clicked, but what I get is that the div only shows for a short while and hides again. Am I doing it correctly with the CSS class or is there anything special about display:none;?

HTML


<div id="message">
    <div class="item-user hid">
        <a href="">something</a> 
    </div>

    <a class="btn-user" href="">button</a>
</div>

CSS


.hid {
    display: none; 
}

JS


<script>
    //jquery is loaded already

    $(document).ready(function(){

        $('#message .btn-user').click(function(){
            $('#message .item-user').removeClass('hid');
        });

    });
</script>
like image 982
TonyTony Avatar asked May 08 '26 16:05

TonyTony


2 Answers

You need to use e.preventDefault() to prevent the default action of your anchor:

$('#message .btn-user').click(function(e){
    e.preventDefault();
    $('#message .item-user').removeClass('hid');
});

Fiddle Demo

like image 196
Felix Avatar answered May 11 '26 06:05

Felix


use this http://jsfiddle.net/3Cp75/

for

<div id="message">
    <div class="item-user hid">
        <a href="">something</a> 
    </div>

    <a class="btn-user" href="">button</a>
</div>

why we are using preventdefault:

  **Prevent a submit button from submitting a form**

otherwise if you dnt want it remove href

link http://jsfiddle.net/3Cp75/1/

like image 23
Avinash Garg Avatar answered May 11 '26 05:05

Avinash Garg