Call a function in javascript on all onclick events - javascript

I want to call a function in javascript. I want to call this function on click of every events or at least on click of all the onclick events in the document.
Please let me know how to do this.

<html>
<head>
<script type="text/javascript" language="JavaScript">
<!--
document.onclick = myClickHandler;
function myClickHandler() {
alert("The document was clicked!");
}
-->
</script>
</head>
<body>
click anywhere within this document.
this will fire the handler too!
</body>
</html>

Related

how can i attach an event to a class

I had link and it automatically has a binding with jquery modal window which when i click, it opens the popup, i want to attach another event to it but it is just ignoring my code
i have this in my code just to see if i can execute the command or not, once it does, i want to call a coldfusion function to run
<script type="text/javascript" defer async>
$(document).on('click',".info",function() {
alert("hello");
});
</script>
if i get an hello,
then i am going to do this
<script type="text/javascript" defer async>
$(document).on('click',".info",function() {
<cfoutput>#runmyfunction()#</cfoutput>
});
</script>
Instead of calling another onClick, why not call the function within the existing onClick?
<script type="text/javascript" defer async>
$(document).on('click',".info",function() {
alert("hello");
otherClickFunction();
});
function otherClickFunction() {
<cfoutput>#runmyfunction()#</cfoutput>
}
</script>

Having trouble with a beginner dom manipulation exercise while using addEventListener

<html>
<head>
<title>Dom Manipulation intro</title>
<!-- <link rel="stylesheet" href="domManStyle.css"> -->
</head>
<body>
<button id="press">Click me</button>
<p>No one has clicked me</p>
<script type="text/javascript" src="DomManip.js"></script>
</body>
</html>
//JS
var button = document.getElementById("#press");
var paragraph = document.querySelector("p");
button.addEventListener("press", onClick);
function onClick(){
paragraph.textContent="Someone Clicked me.";
}
This is the first Dom Manipulation exercise I am trying and keep getting an error: Cannot read property addEventListener of null. I have also tried to set up the JS .addEventListener like:
button.addEventListener("press" , function(){
paragraph.textContent="Someone Clicked me.";
});
but still get the same error.
First, you have a typo in your getElementById method. You have to call it without # as it is already an id and so the id-identifier is not needed:
var button = document.getElementById("press");
Second, there is no press event, you want to listen to a click:
button.addEventListener("click", onClick);

addEventListener() not responding

I have written a small program to pop up an alert on click of a button using an addEventListener() . PFB the code below:
html file
<html>
<head>
<title>name alert</title>
<script type="text/javascript" src="test.js"></script>
</head>
<body>
<input id="p" type="button" value="alert">
</body>
</html>
javascript file
document.getElementById("p").addEventListener("click",greet,true);
function greet(){
alert("hello there !");
}
I dont get a pop up screen this way both the files are in the same folder btw.
test.js loads and executes before the DOM is loaded. So, at that point #p returns null. Try listening to onload event, and add click event.
document.addEventListener("DOMContentLoaded", function(){
document.getElementById("p").addEventListener("click",greet,true);
});
// global scope
function greet(){
alert("hello there !");
}
<input id="p" type="button" value="alert">
try to move your
<script type="text/javascript" src="test.js"></script>
just before
</body>
You need to put the addEventListener inside a function that is called on the load event. As the page is being created it will run the javascript as it comes to it. If it hasn't created the HTML yet, when the addEventListener runs it won't find the 'p' element and it will give you an error.
You can also move the javascript to be after the html, but it's more organized to put it in something like this:
<script type="text/javascript">
window.addEventListener('load', 'function(){pageload()}', false)
function pageload()
{
document.getElementById("p").addEventListener("click",greet,true);
}
</script>

how to click an anchor tag from javascript or jquery

Have an anchor tag, trying to click it from the javascript but not responding, while loading the page it should go to next.php without click anchor tag manually.how can I archive this?
<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="jquery-2.0.3.js"></script>
<script type="text/javascript">
alert("hai");
$(document).ready(function() { $('#about').click(); });
</script>
</head>
<body>
<div>
click here
</div>
</body>
</html>
Use $('selector')[0], as $('selector') returns a jQuery object, so $('selector').click() will fire the click handler, while $('selector')[0].click() would fire the actual click.
$(document).ready(function () {
$('#about')[0].click(); //$('#about').get(0).click();
});
Demo
You can not use javascript to fire click event
It should use
<script type="text/javascript">
$(document).ready(function() {
document.location.href='/next.php'
});
</script>
Here is the code that can help you
$(document).ready(function() {
$(document).ready(function() {
$('a').trigger('click');
});
})
function abc() {
alert("");
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
click me not
If you want the recommended way then use this inside $(document).ready
window.location="where ever you want to go";
If you want the page to go to next.php without clicking then place the window.location directly into $(document).ready(function() { });
Click() function is used when you want to trigger click event. ie. when you want to trigger some action when anchor tag is clicked.
<script>
alert("hai");
$(document).ready(function() {
window.location = 'next.php'; //If you wish the page to automatically go to next page on page load.
$('#about').click( // If you wish to do something clicking anchor
function(){
alert("Hello");
});
});
</script>
$(document).ready(function() { $('#about').trigger('click'); });
Updated:
$(document).ready(function() { $('#about')[0].click(); });

Attaching onclick event to element is not working

I just started learning Javascript, and I know next to nothing. I am trying to attached an onclick event to an element in my HTML.
var joinList = function() {
alert("This should display when clicked");
}
document.getElementById("header").onclick = joinList;
This is my code so far. Nothing happens when the element with the ID of header is clicked on. What am I doing wrong?
the following is my HTML code
<!DOCTYPE html>
<html>
<head>
<title>Testing Page</title>
<script src="testing.js"></script>
</head>
<body>
<h1 id="header">Andrew Dawson</h1>
</body>
</html>
The issue is, that you try to load a html element, which does not "exists" when the javascript function is executed, because the dom has not finished loading.
To make your code work, you can try following solutions:
Place your script tag below in the HTML:
<!DOCTYPE html>
<html>
<head>
<title>Testing Page</title>
</head>
<body>
<h1 id="header">Andrew Dawson</h1>
<script src="testing.js"></script>
</body>
</html>
Add an event handler to check if the window element is ready:
window.addEventListener("load", eventWindowLoaded, false);
function eventWindowLoaded(){
var joinList = function() {
alert("This should display when clicked");
}
document.getElementById("header").onclick = joinList;
}
Another solution would be to use jquery framework and the related document ready function
http://api.jquery.com/ready/
I think the solve you are looking for is
var joinList = function() {
alert("This should display when clicked");
}
document.getElementById("header").setAttribute("onclick", joinList);
Your code seems straight forward, maybe your script is running before the DOM fully loads. To keep it simple across all browsers we can place a self executing anonymous function at the end to initiate all your scripts after DOM loads.
<html>
<title></title>
<head></head>
<body>
html here!!
<script>
(function() {
//Any other scripts here
var joinList = function() {
alert("This should display when clicked");
}
document.getElementById("header").onclick = joinList;
})();
</script>
</body>
</html>
The above is purely javascript, not to be confused with the shorthand (see below) of the jquery "document onready" function (you would need to add jquery to your pages).
$(function() {
//your javascript code here
});
Why using self executing function?

Categories