Auto trigger button action instead pressing it - javascript

I have someone done the below aspx file
Right now the button action need manually triggered by pressing "search" button, I want to achieve that button page to be automatically trigger the action itself without needing person to clicked it, how to I accomplish this?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript" src="scripts/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="scripts/CheckBookingApp.min.js"></script>
<script>
$(document).ready(function () {
$("#btnSearch").CheckBooking();
})
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<button id="btnSearch">Search</button>
</div>
</form>
</body>

Just call your checkBooking() function inside your domready.
You can have it so the popup is displayd when the button is clicked as well.
$(document).ready(function() {
function checkBooking() {
alert('Checking booking');
}
// when the page loads
checkBooking();
//when the button is pressed
$("#btnSearch").on('click', function() {
checkBooking();
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<form>
<button id="btnSearch">Check booking</button>
</form>

you can trigger a "simulated click" event by javascript $('btnSearch').trigger('click');
if you wish to incorporate a time delay you can use setTimeout()
in your example:
$(document).ready(function () {
$("#btnSearch").CheckBooking();
setTimeout(function() {
$("#btnSearch").trigger('click');
}, 5000 /* 5000ms = 5sec */);
});

Related

HTML JS trigger button enter

Was trying to get this working that I saw from another post here. I got it working but I can get the enter to work. I tried to tie in https://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_trigger_button_enter but for the life of me cant get both to work.
Hope someone can help!
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Untitled Page</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#button').click(function(e) {
var inputvalue = $("#input").val();
window.location.replace(" http://www.example.com/page/"+inputvalue);
});
});
</script>
</head>
<body>
<input type="text" value="11" id="input">
<button type="button" id="button">Click Me!</button>
</body>
</html>
just use the keypress event handler from jquery on the document:
<script type="text/javascript">
$(document).ready(function(){
$(this).keypress(function(event) {
if(event.which == 13) {
updateValue();
}
});
$('#button').click(function(e) {
updateValue();
});
function updateValue() {
var inputvalue = $("#input").val();
window.location.replace(" http://www.example.com/page/"+inputvalue);
};
});
</script>

javaScript function doesn't work in first time click

I'm trying to invoke a JavaScript function (that have some php code ) when clicking a button..
but the function doesn't work in first time!! I should click two times to make it start.
here is my code...
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
<script type="text/javascript">
function f1(id)
{
document.getElementById("hiddenVal").value = id;
window.alert("the function is started");
window.alert(id);
f2();
}
function f2()
{
<?php
$Biid=$_POST["hiddenVal1"];
?>
window.alert("<?php echo $Biid; ?>");
}
</script>
</head>
<body>
<form method="post">
<button id="2" onclick="f1(this.id)"> click me </button>
<input type="text" name="hiddenVal1" id="hiddenVal" /> </div>
</form>
</body>
</html>
the problem is that the default behavior of a button inside a form is to submit that form
So you can add a type attribute to the button so it won't submit the form or use a event listener with a "preventDefault" function just like this:
Option 1:
<button id="2" type="button" onclick="f1(this.id)"> click me </button>
Option 2:
document.addEventListener('submit', function(event){
event.preventDefault();
f1(event.target.id);
});

How to call jquery plugin on page load

I am trying to call a jquery (avgrund) modal window plugin on page load but I am not sure how to do it, here is what I have so far; the page for the plugin calls the plugin on button click but I wan't it to execute on page load...
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="en" class="no-js">
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="AvgrundModal/avgrund.css" />
</head>
<body>
<div id="avtest">NO RESULTS</div>
<!-- Include JavaScript file -->
<script src="AvgrundModal/jquery.avgrund.js"></script>
<script>
$(document).ready(function(){
$('#avtest').avgrund();
});
</script>
</body>
</html>
The link to the plugin is here...
http://labs.voronianski.com/jquery.avgrund.js/
Change this:
$('#avtest').avgrund();
for this:
$('#avtest').avgrund().trigger("click");
Just trigger the click event after initialization.
$(document).ready(function(){
$('#avtest').avgrund().click();
});
You're missing jQuery library here:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="AvgrundModal/jquery.avgrund.js"></script>
<script>
$(document).ready(function(){
$('#avtest').avgrund();
});
</script>
and also try to change:
$('#avtest').avgrund();
to:
$('#avtest').avgrund().click();
$(function() {
$('#avtest').avgrund({
height: 200,
holderClass: 'custom',
showClose: true,
showCloseText: 'Close',
enableStackAnimation: true,
onBlurContainer: '.container',
template: '<p>So implement your design and place content here! If you want to close modal, please hit "Esc", click somewhere on the screen or use special button.</p>' +
'<div>' +
'Avgrund on Github' +
'Twitter' +
'Dribbble' +
'</div>'
});
$('#avtest').click();
});
openOnEvent option worked for me.
$(document).avgrund({
openOnEvent: false
});

Jquery Dialog with asp.net

When i click on button to show the message i get one MessageBox that's normal but when i call the Jquery Dialog for the SCOND time i get two MessageBoxs! and when i call Jquery Dialog for the third time i get three MessageBoxs! Please help me!
Here is my Code:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.9/jquery-ui.js" type="text/javascript"></script>
<link href="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.9/themes/start/jquery-ui.css"
rel="stylesheet" type="text/css" />
<script type="text/javascript">
$(document).ready(function () {
$("[id*=Call_Dialog]").click(function() {
$("#MyDiv").dialog({
title: "jQuery Dialog Popup",
buttons: {
Close: function () {
$(this).dialog('close');
}
}
});
$("#ShowMessage").click(function () {
event.preventDefault();
alert('Hi');
$("#MyDiv").dialog('close');
})
})
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div id="MyDiv" style="display: none">
<asp:Button ID="ShowMessage" runat="server" Text="Button" />
</div>
</form>
<p>
<input id="Call_Dialog" type="button" value="button" /></p>
</body>
</html>
Thanks, but how can i call the dialog with button click?
Dialog should be initialized only once not every time you click on something and regenerate
solution is put dialog initialization outside of click and you're done
$(document).ready(function () {
$("#MyDiv").dialog({
title: "jQuery Dialog Popup",
buttons: {
Close: function () {
$(this).dialog('close');
}
}
});
$("[id*=Call_Dialog]").click(function() {
$("#ShowMessage").click(function () {
event.preventDefault();
alert('Hi');
$("#MyDiv").dialog('close');
})
})
});
to open jquery dialog : use this overload :
$("#MyDiv").dialog('open');

Differences between window.onload/onunload and body.onload/onunload

I have read the answers for the question window.onload vs <body onload=""/>. In that Q&A, many claim that window.onload and body.onload are identical. This is not what I experience.
Consider the two test pages:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>test 1</title>
<script type="text/javascript">
var initialSelectedIndex = 0;
function resetMenu()
{
document.getElementById("fruitMenu").selectedIndex = initialSelectedIndex;
}
</script>
</head>
<body onload="resetMenu();" onunload="resetMenu();">
<br />
<select id="fruitMenu">
<option value ="apple">apple</option>
<option value ="banana">banana</option>
<option value ="strawberry">strawberry</option>
<option value ="grape">grape</option>
</select>
<p>google
</p>
</body>
</html>
And:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>test 2</title>
<script type="text/javascript">
var initialSelectedIndex = 0;
function resetMenu()
{
document.getElementById("fruitMenu").selectedIndex = initialSelectedIndex;
}
window.onload = resetMenu();
window.onunload = resetMenu();
</script>
</head>
<body>
<br />
<select id="fruitMenu">
<option value ="apple">apple</option>
<option value ="banana">banana</option>
<option value ="strawberry">strawberry</option>
<option value ="grape">grape</option>
</select>
<p>google
</p>
</body>
</html>
With the "test 1" page, if you select an item from the drop down menu and the click the link to navigate away from the page and then hit the back button the menu will be reset to its initial state. This doesn't happen for the "test 2" page though. Why?
While this is a test, my goal is to do something similar on an aspx page using RegisterStartupScript or RegisterClientScriptBlock so I want to be able to recreate the behaviour of "test 1" without using the body onload/onunload but using window.onload/onunload.
Crescent is correct, by using:
window.onload = resetMenu();
You are making window.onload equal to the return value of the resetMenu function, rather than providing the function which should be called onload (and unload). So you should use:
window.onload = resetMenu;
window.onunload = resetMenu;
But why do you need to reset the menu when the page is unloaded?
Note: You can also use anonymous functions as the onload handler :)

Categories