hello, iam very new in jQuery. Need help asap please. on my asp.net page I want to use two jQuery functions.

1. Clears textBox when user clicks on it.
2. Clears all inputs when user clicks on btnCancel button.

I made two functions

//clears value from onclicked input
        $(document).ready(function () {
            $("input").click(function () {
                $(this).val(""); //clears value from onclicked input
            });
        });

        //Clears all when reset button is pressed
        $(document).ready(function () {
            $("#btnCancel").click(function () {
                $('input').val("");
            });
        });

The problem that this code won't work together. Perhaps I do not combine it in the right way? Please help!

Recommended Answers

All 10 Replies

Dumb question but are you sure you gave the cancel button an id of btnCancel?

Also you should have that first function check to see if the user has entered text. You should also change .click to .focus on the first function because if for some reason they click the input field even if it is focused it will erase the text.

Check the ID as xylude states. ASP.NET changes them when you run you're page. You're better off using a class.

thank you for answer

function check to see if the user has entered text.

No need because textbox already has value (hint what is this)

You should also change .click to .focus on the first function because if for some reason they click the input field even if it is focused it will erase the text.

Changed. It works the same way. At least in Safari. What is strange for me...is why they won't work toger. I mean when they stay like now none of them work.

P.S The jQuery placed in masterpage. in <head> section.

Check the ID as xylude states. ASP.NET changes them when you run you're page. You're better off using a class.

Pardon, what's that?

xylude hinted on the id of your link.

I noticed when working with asp.net and jquery that using a class to reference items, instead of their id, often works better.

xylude hinted on the id of your link.

I noticed when working with asp.net and jquery that using a class to reference items, instead of their id, often works better.

can you give me an example?

well, how to combine them working together?

Add class="btnCancel" to your link and use $(".btnCancel").click(...); Also, though not a problem, one document ready function will suffice.

Add class="btnCancel" to your link and use $(".btnCancel").click(...); Also, though not a problem, one document ready function will suffice.

<asp:Button ID="btnCancel" runat="server" CssClass="btnCancel" Text="בטל" />

this is the code of my button. Do you mean to add class="btnCancel" ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.