How do i trigger a jquery event [duplicate] - javascript

This question already has answers here:
How to open a file / browse dialog using javascript?
(9 answers)
Closed 9 years ago.
i have this problem:
i have this code
if(ext!="rar") {
$('#myform').trigger('click');
}
and if the file extension of the chosen file is not rar, the file input should open the window again to choose another file, but this code does not seem to work. What should i do?

$("#btn").click(function()
{
var fu = $("#fu");
var ext = /[^.]+$/.exec(fu.val());
if (ext!="rar")
{
fu.trigger("click");
}
});
http://jsfiddle.net/tugpM/1/

Assuming that your form tag has the id myform, what you have done just triggers a click on the form (not even on the submit button of the form). Without the HTML, it is not possible to know what you are triggering the click on.
What you need to do is trigger the click on the submit button
$('#mysubmitbutton').click();
or trigger a form submit:
$('#myform').submit();

Related

Javascript Dom button onclick function working on page load why? [duplicate]

This question already has answers here:
onclick function runs automatically
(4 answers)
Closed 2 years ago.
HI i am new to js and html,
i am trying to add button on html through js and on button click i tried show alert message
here My code
topobutton=document.createElement("button");
topobutton.innerHTML="Topo";
topobutton.value="Topo";
topobutton.type="button";
topobutton.id="Topo";
topobutton.style.margin="2px";
document.body.appendChild(topobutton);
topobutton.onclick=showMap("topo");
function showMap(mapname){
alert(mapname)};
when page is reloading it shows the alert message and on button click not responding why?
EDIT
You calling the function instead of add reference / event listener.
topobutton.onclick = () => { showMap("topo") }
// OR
topobutton.onclick = function() { showMap("topo") }
// OR
topobutton.addEventListener('click', function() { showMap("topo") });

How to send input/select fields within a form without refreshing page (PHP/AJAX/JS) [duplicate]

This question already has answers here:
How to prevent buttons from submitting forms
(20 answers)
Closed 4 years ago.
I have few field within a form to dynamically add data to database. I send data from couple fields to php using ajax and receive correct response. But when i click button with attached click handler to submit required fields whole form being reloaded i dont know why. Could somebody please help me out. Anyway thanks in advance ;)
$("#button").click(
function(){
var somedata1= $(".someinput").val();
var somedata2= $(".someselect").val();
ajax_post('http://someurlhere',
'some1='+somedata1+'&some2='+somedata2+'&csrf_token='+csrf_token, handle_submit, handle_error);
}
);
You need to prevent the default action that submitting a form does (reloading the page).
$("#button").click(
function(e){
// this prevents the default action from the submit action
e.preventDefault();
var somedata1= $(".someinput").val();
var somedata2= $(".someselect").val();
ajax_post('http://someurlhere',
'some1='+somedata1+'&some2='+somedata2+'&csrf_token='+csrf_token, handle_submit, handle_error);
}
);

jquery how to select other element that is inserted by other JS/jQuery [duplicate]

This question already has answers here:
Event binding on dynamically created elements?
(23 answers)
Closed 6 years ago.
I am running discourse and I am trying to modify what happens when one clicks the logout link, I do not want to modify discourse stock javascript...
There is an element elem1 that when clicked inserts another html menu with the logout link. So is it not present when $(document).ready.. is called. I need to modify what happens on elem2.click which is where the logout link is.
I want to do this...
$("#elem1").click(function() {
console.log("step1")
$("#elem2").click(function () {
console.log("step2")
..do my custom logout
})
})
but I think jquery is not finding it because it was not there when it first loaded.
Change to this
$("#elem2").on('click',function () {
console.log("step2")
..do my custom logout
})
or if you're using older jQuery:
$("#elem2").live('click',function () {
console.log("step2")
..do my custom logout
})

onclick submit button JSP [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Multiple submit Button click problem?
Have a type=Submit button in a JSP. Requirement is to disable this button on click.
The basic requirement is to stop posting multiple requests of the same form if this button is clicked multiple times for the same form.
Thank You.
Even if you disable the submit button the user can still submit the form by pressing F5 or ctrl+F5 or even using Browser reload button.So disabling the submit button won't be of much use.
Why not use Post/Redirect/Get method.
The PRG pattern basically redirects the user to another page when it is Posted.
Hence now when the user reloads or tries to resubmit the form he is calling the Get method so the form is prevented from resubmission.
However this is also not fully secured approach.
Look here: http://www.developertutorials.com/tutorials/javascript/how-disable-submit-button-050416-1275/
I use a simple JS function you can invoke with onlick():
<script type="text/javascript">
var isSubmitted = false;
function safeSubmit(entityName) {
if (!isSubmitted) {
isSubmitted = true;
document.forms[0].submit;
}
}
</script>

Can i detect/capture the Broswer Back button using javascript? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Is there a way to catch the back button event in javascript?
In my application,the user can enter data eithe add or modify those modification is saved into database using ajax.After all the modifications is completed in the screen then user will be finalized the data.In the time,user accediently click on Browser 'Back' Button.
So,what i need is,if user clicked without 'Update' button then if clicks the Browser 'Back' button.i want to shown the alert message.So,How to detect/capture the 'Back' Button.Please help me
You could use the window.onbeforeunload event
<body onunload="Unloader()">
<script type="text/javascript">
function Unloader() {
}
</script>

Categories

Resources