0

I'm using JQuery 1.5.2.

Whilst using the .show function, it only shows the element partly, for like 0.3 seconds, and then the element hides itself again. This is consistent, with all other forms. What am I doing wrong?

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="Scripts/jquery-1.5.2.js" type="text/javascript"></script>

<script type="text/javascript">
    $(document).ready(function () {

        $('#Button1').click(function () {
            $('#clickmeupdate').show(function () {

            });
        });

    });
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
    <span style="background-color:Blue; display: none" id="clickmeupdate" >Click me to update</span>
        <asp:Button ID="Button1" runat="server" Text="Button" />
    </div>
    </form>
</body>
</html>
3
  • I don't use ASP's server controls, but I'm kinda guessing that runat="server" on the button comes into it... Commented Apr 2, 2011 at 18:05
  • I think TJ has a point here. Look at the 'view source' the button ID might be different. It that is the case the use the ClientId property refer to the Id. Commented Apr 2, 2011 at 18:08
  • cheers mate, the asp control doesnt work, but html works just fine Commented Apr 2, 2011 at 18:09

3 Answers 3

2

The button is posting back and when it does the display is set back to none.

You can stop the postback by not using an asp.net button, just use an html button or by adding an onClick event and return false.

EDIT - I've adjusted my answer since in the comments you mentioned you needed the button click event.

You can still do this by using the OnClientClick instead to run the jQuery. See this for more info.

However, you still have the issue of the postback resetting the span to not display since the OnClientClick occurs first and then the onClick.

I think you really want to setup an AJAX kind of solution for this OR just have your span be set to runat=server and in your code behind of the button click set it to visible.

I suspect though you would be better served to check out the UpdatePanel / AJAX stuff in Asp.Net web forms. You could do all this with jQuery AJAX solution but it may be easier for you to use the web forms UpdatePanel.

Sign up to request clarification or add additional context in comments.

1 Comment

but the problem is that i am calling a function in button1_click event also
1

I think your button is posting back and causing the page to refresh, if you add return false to the onclick function it should get rid of the problem.

2 Comments

but i need to call a function at the same time
ah, I see. In that case you'll probably want to hide it on the postback.add runat=server and an id to the span. You could also probably remove 'display: none' and add visible=false. Then, when the button posts back set visible=true on the span.
-1

Try this:

$('#Button1').click(function () {
        $('#clickmeupdate').show();
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.