codefreak
codefreak

Reputation: 309

Not able to hide a div on a button click using js

I have 4 divs and 4 different buttons and i want to hide a particular div when its corresponding button is pressed.


my asp code is

<div class="div2">
    <div class="d1" id="one">
        <h3 class="h3">First Division</h3>
        <hr color="black" />
    <p class="text-green">
        //some text

    </p>
        <br />
        <asp:Button class="btn" ID="Button1" runat="server" Text="Click Me to Hide" />
    </div>
    <div class="d1" id="two">
        <h3 class="h3">Second Division</h3>
        <hr color="black" />
    <p class="text-red" >
        //some text
    </p>
        <br />
        <asp:Button class="btn" ID="Button2" runat="server" Text="Click Me to Hide" />
    </div>
    <div class="d1" id="three">
        <h3 class="h3">Thrid Division</h3>
        <hr color="black" />
    <p class="text-blue">
        //some text
    </p>
        <br />
        <asp:Button class="btn" ID="Button3" runat="server" Text="Click Me to Hide" />
    </div>
    <div class="d1" id="four">
        <h3 class="h3">Fourth Division</h3>
        <hr color="black" />
    <p class="text-yellow">
        //some text
    </p>
        <br />
        <asp:Button class="btn" ID="Button4" runat="server" Text="Click Me to Hide" />
    </div>

and my js code is

$(document).ready(function () {
$("#Button1").click(function () {
    $("#one").hide('slow');
});
$("#Button2").click(function () {
    $("#two").hide('slow');
});
$("#Button3").click(function () {
    $("#three").hide('slow');
});
$("#Button4").click(function () {
    $("#four").hide('slow');
});
});

But nothing is happening on button clicks. What is the reason of such error??

Upvotes: 0

Views: 168

Answers (3)

Shekhar Pankaj
Shekhar Pankaj

Reputation: 9125

Add ClientIDMode="Static" to your asp:button, something like this:

<asp:Button ID="Button1" runat="server" ClientIDMode="Static" Text="Button" />

This will make the ID remain the same. It disables the autogenerated names for this control.

Here is the reference for this: http://msdn.microsoft.com/en-us/library/system.web.ui.control.clientid.aspx

Working Fiddle..have a look on below link it is working Fine Fiddle

Upvotes: 2

afzalulh
afzalulh

Reputation: 7943

Simply change your JQuery to this:

$(document).ready(function () {
    $('.btn').on('click',function (e) {
        e.preventDefault();
        $(this).closest('.d1').hide('slow');
    });
});

Upvotes: 0

Chris Brickhouse
Chris Brickhouse

Reputation: 648

make sure to put e.preventDefault() in each error handler. for example

$("#Button1").click(function (e) {
    e.preventDefault();
    $("#one").hide('slow');
});

if you don't do this, it will cause an asp.net postback as these buttons are inside the form tag. and like another user said, remove runat="server", as this can change your id's.

you don't even need asp:button for this, you can just use html.

<button id="#Button1">Button Label</button>

Upvotes: 0

Related Questions