Call submit button function from jquery? - javascript

I want call OnClick function on page load with out user clicking. Can we do that in jQuery or javascript?
<input name="ctl00$PlaceHolderMain$ButtonSection$RptControls$BtnSubmit"
class="ms-ButtonHeightWidth"
id="ctl00_PlaceHolderMain_ButtonSection_RptControls_BtnSubmit"
accessKey="o" onclick="javascript:WebForm_DoPostBackWithOptions(new
WebForm_PostBackOptions("ctl00$PlaceHolderMain
$ButtonSection$RptControls$BtnSubmit",
"", true, "", "", false, true))"
type="button" value="OK"/>

$(document).ready(function() {
$("#ctl00_PlaceHolderMain_ButtonSection_RptControls_BtnSubmit").click();
});
This will trigger the click event on the element with the supplied id, and it will run when the document is fully loaded.

You can call trigger and pass the type of event to trigger.
$('#ctl00_PlaceHolderMain_ButtonSection_RptControls_BtnSubmit').trigger('click');

You can try doing a $("#ctl00_PlaceHolderMain_ButtonSection_RptControls_BtnSubmit").trigger('click'); This would emulate a click on the button more info here

You imply in the question title that the button you want to click is a submit button. If so you would be better off calling the submit method of the form instead of the click event of the submit button.
document.forms["myform"].submit()

Or with javascript inside the body:
<body onload="javascript:document.getElementById('ctl00_PlaceHolderMain_ButtonSection_RptControls_BtnSubmit').click()" ></body>
The trick is to get the element and call the click method.
Greetings.

var $btnSubmit = $('#ctl00_PlaceHolderMain_ButtonSection_RptControls_BtnSubmit');
// Either method will work:
// Method 1: jQuery has a click event pre-defined
$btnSubmit.click();
// Method 2: some events (such as newly defined HTML5 events)
// may not be pre-defined, so .trigger('[event]') is a way to
// explicitly invoke that event
$btnSubmit.trigger('click');

Related

Update button onClick url from AJAX in Laravel

I have a simple form with a "Submit" button and an additional "Add" button in blade template. On form submit there is an ajax callback to a controller which validates provided value. Based on the returned result from the controller, I'd like to change the "Add" button onClick event. Here is blade snip:
<button id="theId" type="button" >Add</button>
Relevant ajax pice:
$.ajax({
...
success: function(result){
//Update onClick for the add button
addButton.onclick=function () {
//location.href = 'www.example.com';
window.location="www.example.com";
};
addButton.html("aNewName");
}
}
Based on all the sources either location.href or window.location should redirect me to a different page (in my case just a different subpage) but it does not. After hours of research I thought that I get a wrong button or sth is wrong with the ajax itself but then I added addButton.html line and this works well. What do I do wrong here?
Get the button, remove eventually previous click events, add the new event using the http/https prefix, change the text of the button
success: function(result){
var button = document.querySelector('#theId');
button.addEventListener('click', function () {
window.location = 'https://www.google.it';
});
button.innerHTML = "my new button text";
}
From the MDN docs here: https://developer.mozilla.org/en-US/docs/Web/Guide/Events/Event_handlers#Registering_on-event_handlers, a handler registered via an on* attribute will be available via the corresponding on* property, but not the other way around.
What this means is that the onclick attribute of the div remains the old event handler in HTML.
To keep sanity, always register event listeners with the addEventListener method.
It looks like you are trying to set a jQuery object's onclick property to a function, which is not the right way to set a click event handler in jQuery.
You should use the jQuery object's click method to set a click handler. And use the off method to remove the previous handler:
addButton.off('click');
addButton.click(function () {
window.location="https://www.example.com";
})
Here's a working fiddle.

How do I handle the event of an element getting added to the page?

I have an app where clicking a link brings up a modal. In the modal is a form. I need to monitor for that form's submission.
I'm attaching a click handler in my JS like so:
$('.vex-dialog-form :submit').click (event) ->
alert "hi"
That alert isn't firing, I believe it's because I need to attach some kind of event handler for that modal loading then put the submit event inside of that.
Any suggestions on how to go about this?
you need event delegation for dynamically added DOM. use .on():
$('.vex-dialog-form').on('click',':submit',function(){
alert("hi");
});
if parent .vex-dialog-form is also getting added dynamically, then use:
$(document).on('click','.vex-dialog-form :submit',function(){
alert("hi");
});
Try this way:
$(document).on('click', '.vex-dialog-form :submit', function(){
alert('hi');
});
In this case document is waiting for click and after it gets clicked, jQuery finds out whether your submit button is clicked or not

preventdefault not preventing when its inside button

I could not make preventdefault to prevent action. I apologize if the answer is too easy but I simply cant find the error. why is it not preventing from entering the link? jsfiddle given below.
http://jsfiddle.net/zwY5p/34/
$('#theForm').click(function(e) {
event.preventDefault();
alert('FORM!');
});
e != event
$('#theForm').click(function(event) {
event.preventDefault();
alert('FORM!');
});
The parameter passed to the handler function is what you need to execute preventDefault on. In your code, you are passing e but calling preventDefault on event.
preventDefault prevents the default browser action. It does not cancel the inline JavaScript, since that runs first. If you have to override that, just remove it (no event listener necessary):
$('#theForm').removeAttr('onclick').
your event parameter name e and the variable you are using event are different,
$('#theForm').click(function(event) {
event.preventDefault();
alert('FORM!');
});
Other than the errors pointed out on other answers there's another small issue, specifically in your markup declaration:
<!-- Use either the closing tag or the slash (/) in the opening tag -->
<button id="theForm" onclick="location.href='http://www.example.com'" />
go to google
</button>
On the topic, you have two different handlers attached to the button element, they are both handling the click event but they are still different and separate things. jQuery won't know about the handler defined in the markup:
var btn = document.getElementById('theForm');
jQuery._data( btn, "events" );
will return an array with a single element which is the handler added via jQuery.
Now you have to re-evaluate the need of two different handlers for the same element and event and apply conditions. Do you really need to do it this way?
You're using 2 'click' events.
You end up using preventDefault once, and it's used after the 1st click event has ran.
If you make your button an href, then your preventDefault will be working.
It will also make more sense, as the JS will be separated from the HTML markup.
Also, of course you must use the same parameter name. (function(event), with event.preventDefault for example).
If you are passing "e" as an event to the function then you should prevent the default action only for that "e" that you have passed and not for "event".
$('#theForm').click(function(e) {
e.preventDefault();
alert('FORM!');
});
jQuery preventDefault() method: http://api.jquery.com/event.preventDefault/

Override jquery onclick event?

I have a button in ASP.NET, that renders to HTML as such:
<input id="btn" type="submit" value="clickme">
If I then add the jquery:
$('#btn').click(function(){return false;});
Every time the button is clicked, nothing will happen (i.e. no postback).
This is fine.
Is there any way in Javascript I can programatically invoke the click (which will cause a postback) whilst also disregarding the jquery-attached, return false function?
You can have the event handler accept additional arguments. When triggering the handler with .trigger you can specify values for these arguments, which will let the handler modify its behavior accordingly.
For example:
$("#btn").click(function(event, submitForm) {
submitForm = submitForm || false;
if (!submitForm) return false;
});
// This will not submit the form
$("#btn").trigger("click");
// But this will
$("#btn").trigger("click", true);
The button itself does nothing by default except submitting the form, so try this:
$('#btn').closest('form').submit();

How to remove all click event handlers using jQuery?

I'm having a problem. Basically, when a user clicks an 'Edit' link on a page, the following Jquery code runs:
$("#saveBtn").click(function () {
saveQuestion(id);
});
By doing this, the onClick event of the save button calls the saveQuestion() method and passes on the ID of the question for which the 'Edit' link was clicked.
But if in the same session the user clicks edit on 2 questions, then instead of overwriting the previous click event handler, it instead causes 2 event handlers to run, one which might call saveQuestion(1) and the other might call saveQuestion(2). By doing this 1 question overwrites the other.
Is there a way to remove all previous click events that have been assigned to a button?
You would use off() to remove an event like so:
$("#saveBtn").off("click");
but this will remove all click events bound to this element. If the function with SaveQuestion is the only event bound then the above will do it. If not do the following:
$("#saveBtn").off("click").click(function() { saveQuestion(id); });
Is there a way to remove all previous click events that have been assigned to a button?
$('#saveBtn').unbind('click').click(function(){saveQuestion(id)});
$('#saveBtn').off('click').click(function(){saveQuestion(id)});
If you used...
$(function(){
function myFunc() {
// ... do something ...
};
$('#saveBtn').click(myFunc);
});
... then it will be easier to unbind later.
$('#saveBtn').off('click').on('click',function(){
saveQuestion(id)
});
Use jquery's off and on

Categories

Resources