I show a page in a jQuery dialog box. The user fills in the fields and submits the form.
In the function I need to call e.preventDefault(). In the underlying code I used the RegisterStartupScript method on the submit button. I know how to do it by passing parameters:
System.Web.UI.ScriptManager.RegisterClientScriptBlock(Page, GetType(Page),
"Script", "OrderForm('Item.aspx?OrderNum=" & _ID & "');", True)
How can I pass an event object in registerStartupScript?
I want a function like this:
function OrderForm(e, number) {
e.preventDefault();
//do something using number;
}
2 Answers 2
The .click() method is part of a framework (in this case I'm pretty sure it's jQuery) and it accepts a predefined number of arguments. You can use (for example) a data- attribute in your button with the number and then access to it through the object this.
// html
<button data-number="3">my button</button>
// js
$('button').click(function(e){
e.preventDefault();
var number = $(this).data().number; // or $(this).data('number')
alert(number);
});
Try with this jsfiddle: https://jsfiddle.net/q3fvxk93/
8 Comments
$(this).data("number").data() returns an object. It is accessible (also) by object notation..data().something returns just the value for something. Is it less efficient than .data('something')? Sincerely I don't know (I will review it in free time), but probably is the same (talking about performance)..data().something returns the whole data object, then performs an object property access to get the value for just something.the event object is in the scope $(this)
I don't know where number should come from.
So your code would look this
$('btn').click(function(e) {
e.preventDefault();
$(this).attr('id'); //the button and it's properties like attributes in this example I get the ID of the button
});
<btn>. Did you mean for that to be a class or ID?OrderForm()fromRegisterClientScriptBlock(), there is no event to pass because you are calling the method directly. I mean in your code there is no event to handle. See the example from msdn; in that example is possible to pass an event because the user needs to click the button.