Loading javascript function on button event - javascript

I would like to call a Javascript function once the button is displayed on screen. What I am looking for is similar to the 'onclick' attribute:
<input type="button" class="button-class" onclick="function(this)">
However, I would like the function to be called as soon as the button is displayed on screen i.e. it should be instantaneous (button creation=function call). I have already tried 'onload' but this does not seem to work. Is there a way to do this please?
Thank you

Put an script element invoking the function after the button element
<input type="button" class="button-class" onclick="fn(this)" value="button" id="btn">
<script>
function fn(obj){
console.log(obj)
}
fn(document.getElementById("btn"));
</script>

#Questionnaire, you can't do what you want since an action should take place for a button (click event) to execute code.
As a good practice, load your Javascript code after the page is done loading. This is to avoid blocking the rendering of HTML code since
Javascript is synchronous.
...
<script type="text/javascript">
function init() {
// Code for your button function here
}
window.onload = init();
</script>
</html>
The code above is pretty much it.

Just write the function name in onclick property instead of function(this) like the following..
<script>
function myFunc(e){
//do something
}
</script>
<input type="button" class="button-class" onclick="myFunc(this)">

#Bartman pointed out how the .ready() funtion handled it as a document.ready.
So i came up with a small solution to run the waanted funtion once the input button is created. Hotfix but cant imagine another solution
I hope this helps. If not please add a comment so i can edit the answer.
function clickFunc(e) {
alert("click event");
}
function loadFunc() {
alert("load event");
}
$("#button").click(function() {
$("div").append("<input id=\"but\" type=\"button\" class=\"button-class\" onclick=\"clickFunc(this)\">");
$("body").append("<script>loadFunc();<\/script>");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="button" type="button">
<div></div>

Related

How to put a JavaScript function in an HTML onclick

Hello I would like to put my JavaScript function in an onclick html because I have several exercises on my page
<button type="button" onclick="formatTime()">Click</button>
Your button should call the javascript function formatTime() onclick, there is nothing wrong with the HTML. Does the Chrome/Firefox Developer Console output any errors on click?
See here for an example.
<button type="button" onclick="foo()">click</button>
<script>
function foo() {
alert('hello world');
}
</script>

How to Auto Click in HTML Button

<h1>Welcome to My Page!</h1>
<h2>My name is Bob.</h2>
<h3>I hope you like it here.</h3>
<embed src="MY WEBSITE LINK" style="width:500px; height: 300px;">
<input type="checkbox" id="btd" onmouseover="myFunction()" onclick="alert('clicked')">
<script>
jQuery(function(){
jQuery('#btd').click();
});
</script>
I have the code. But didn't Work.
http://www.imagebam.com/image/31ac1b820717083
How to Auto Click Button Continue, from Image?
We can't do auto click in HTML, but we can call any function that the button does with Jquery.
To work Jquery code, you want to add this to your code
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
Actually when we click any button, it calls a function, that may be our own function or built in one.
Here Javascript can easily call any function without any button
Like this
//define the function
function alertsomething(){
alert("Hey, This is an alert inside that function");
}
//function works when button clicks
$("#btd").click(function(){
alertsomething();
})
//function automatically when page loads
$(document).ready(function(){
alertsomething();
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="btd" >Click me</button>
Here are the two ways to call a function. One is when you click the button and another one is when your page loads, that function will call automatically...
Thanks
<script> setInterval(function(){ document.getElementById('btn').click() }, 10000); </script>
Its working script for me
Send "data" from FORM 'POST' to refresh HTML page whith PHP code

Programmatic Form POST from jQuery doesnt work in document ready

I have the below script in my code:
<script src="~/Scripts/jquery-2.2.3.js"></script>
<script type="text/javascript">
$(document).ready()
{
$("#hdnTest").text('#ViewBag.Test');
$("#frmRouter").submit();
};
function submitform()
{
$("#frmRouter").submit();
}
</script>
I also have the below html in my mvc view:
<form id="frmRouter" method="post" action="https://localhost/Destination/Index">
<div>
<input type="hidden" id="hdnTest" name="hdnTestName" />
</div>
Click
</form>
When I click on the hyper link, the form is getting posted to the action URL. However, the form submit inside the document.ready doesn't work. Why is it so?
You've written the code in a way that's syntactically correct, but functionally wrong:
$(document).ready(function() {
$("#hdnTest").text('#ViewBag.Test');
$("#frmRouter").submit();
});
is what you want. The .ready() method should be passed a reference to a function. It doesn't have to be an anonymous function (as above), but that's pretty common.
Your code was missing the function, so jQuery just basically ignored the method call. The subsequent block of code was executed, but since the form element didn't exist in the DOM it didn't do anything.
Your document.ready has not the correct syntax.
You have two ways to perform a document.ready function.
$(document).ready(function () {
$("#hdnTest").text('#ViewBag.Test');
$("#frmRouter").submit();
});
$(function () {
$("#hdnTest").text('#ViewBag.Test');
$("#frmRouter").submit();
});
$(document).ready(function(){
$("#id_of_your_anchor_tag").click(function(){
$("#hdnTest").text('#ViewBag.Test');
$("#frmRouter").submit();
});
});
Click
Try this way,
Hope it helps !

Problem hiding element when button is clicked

I try to hide the text with a button click, not sure how it is done:..
<script type="text/javascript">
$('.HideButton').click(
function () {
$('#disclaimer').hide();
}
);
</script>
The body:
<p id="disclaimer" > DDDDDDDDDDDDDDDDDDDDDDDDDD</p>
<asp:Button ID="Button1" CssClass="HideButton" runat="server" Text="Hide" />
You need to wrap it in the ready handler, but apart from that it should work:
$(function() {
$('.HideButton').click(function () {
$('#disclaimer').hide();
});
});
(demo - slighty changed in order to overcome ASP dependency.) Do note, that the button may have other side-effects, too, cf. #Zootius' answer.
Your button should not be an asp:Button. Do this instead.
<input type="button" value="Hide" class="HideButton" />
This is because the asp:Button causes a full postback on click (check your page source - it renders as a form-submit button).
Put it in "document.ready"
document.ready(function() {
//Your code here
});
Try:
<script type="text/javascript">
$(document).ready(function(){
$('.HideButton').click(function () {
$('#disclaimer').hide();
});
});
</script>
You need to tell the browser when to add the listener to the button. Usually that is done in the ready function because you want it always as soon as the page is rendered.

Script not executed after FORM element

I have an .aspx page. I populate a public variable in the code behind (.cs) page and then access that variable in JS on client side. I have the script declared after the FORM tag as below :-
<body>
<form>
...
</form>
<script language="javascript" type="text/javascript">
debugger;
var data = "<%=cSharpData%>";
</script>
</body>
After postback this script does not get executed first time, but when I click on any other server button on the page, then it gets executed.
Any idea why?
Is your postback done with ajax?
If so then only part of the page is refreshed and the script is not executed again.
You can wrap it in a function and add that function to the postback callback handler.
This is how I did it on one site:
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(function () { edit.update(); });
Putting some Javascript after a <FORM> element does not mean it will get processed after the form has been submitted. It gets processed as the HTML is rendered, which is what you are seeing.
If you want to run some Javascript after POSTing a form then you need to use a onClick handler, something like this:
<FORM NAME="myform" ACTION="" METHOD="GET">
<INPUT TYPE="text" NAME="inputbox" VALUE=""><P>
<INPUT TYPE="button" NAME="button" Value="Click" onClick="dosomejavascriptstuff()">
</FORM>
have you tried to execute it with jquery $(document).ready()?
something like:
<script language="javascript" type="text/javascript">
$(document).ready(function(){....});
</scrip>
updated
without jquery
/* registr event on document load */
if ( document.addEventListener ) {
document.addEventListener( "DOMContentLoaded", log4link, false );
} else if ( document ) {
document.attachEvent("onreadystatechange",function(){
if ( document.readyState === "complete" ) {log4link();}
});}
Thanks for your replies. I got closer to the issue but I do not know the solution :(
The C# variable contains a path info as "D:\" and when I set this on a JS variable I get the error as "Unterminated String Constant".
How do I overcome this.
Actually this was the problem, why the script was not getting executed. As soon as I removed the "\", it worked !

Categories