1

I have to use a user control form

In that form, there is a Submit button.

That button is html button.

That button has lots of validation functions for the other controls.

Now I want to add another function of my own into that button.

Now I faced my problem. I dunno the ID of that button.

When I view source I find no ID or Name.

But I can use

<script type="text/javascript">            
     $("input[type=submit]").click(function () { return validateCaptcha(); })
</script>

To add my function.

If I do that, only my function would run and the other original validation functions are not running anymore.

I want to append my function at the end of its validation functions.

I found this..

JavaScript: Adding an onClick handler without overwriting the existing one from another question

But that one find the controls by tagname. If I find it by tagname there would be lots of input tags.

Is there any other way to combine the 2 answers that I found?

Tkz

1
  • 1
    if validateCaptcha returns false or preventsDefault it will prevent the event from bubbling to other handlers. Other than that, it should work. Commented Nov 8, 2011 at 4:03

3 Answers 3

1

If you know the name or the ID of the form you are working on you could do

$("#formname").submit(function(){return validateCaptcha(); });

or

$("form[name=form_name]").submit(function(){return validateCaptcha();});

Witout knowing the ID of the submit button or the form (or the name of either) it would be difficult to validate; you need some sort of identifier.

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

Comments

0

When you don't have any id or name there is no way to distinguish control from others in page. So, simple solution will be to give id to button placed in user control. And in javascript if placed inside that page you can access it unique Id using ClientID. e.g.

<asp:Button runat="server" id="btnOK" Text="Send Me" />

In JS Script

<script type="text/javascript">            
   $("#<%= btnOK.ClientID%>").click(function () { return validateCaptcha(); })
</script>

2 Comments

I can't touch the ascx. That's why
you can do that by accessing that control in containing page too. e.g. this.FindControl("userControl").FindControl("btnOK").ClientID
0

If it has a class you can also do like this

$(".className").click(function () { return validateCaptcha(); })

or find any other unique attribute of that button.

$("input[uniqueAttribute='uniqueAttributeValue']").click(function () { return validateCaptcha(); })

by doing this kind of selector, make sure the selector results to only 1.

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.