Right now I'm using <body onload="function">
, which changes some text on the page based on which element is focused on. It works fine, but I need my site to run the function every time the focus changes (or, even better, every time any part of the page is clicked).
Currently the code looks like this:
<body onload="changeText()">
<script>
function changeText(){
function here;
}
</script>
<p>This is where text changes based on the function</p>
Thanks!
Right now I'm using <body onload="function">
, which changes some text on the page based on which element is focused on. It works fine, but I need my site to run the function every time the focus changes (or, even better, every time any part of the page is clicked).
Currently the code looks like this:
<body onload="changeText()">
<script>
function changeText(){
function here;
}
</script>
<p>This is where text changes based on the function</p>
Thanks!
Share Improve this question edited Oct 11, 2012 at 17:57 freefaller 20k7 gold badges61 silver badges95 bronze badges asked Oct 11, 2012 at 17:56 swaysway 3732 gold badges5 silver badges17 bronze badges 5 |4 Answers
Reset to default 8You can attach onclick event on document. jsfiddle
document.onclick = function(){
// your code
//alert("clicked");
}
If you want change on focus then use
window.onfocus = function(){
// your code
//alert("focus");
}
window.onclick = function () {
//do some stuff
document.body.style.backgroundColor = "red";
}
<p>text text text</p>
<button>it does not matters if you press me or any where else</button>
Try this:
<!-- language: lang-html -->
<body onload="changeText()">
<script>
function changeText(){
function here;
}
document.onclick=function(){changeText();};
</script>
<p>This is where text changes based on the function</p>
jQuery doesn't suffer from the disable-clicking issue
$(document).click(function(e){
//Blah
})
onclick
to. – Robert Harvey Commented Oct 11, 2012 at 17:58Searching SO I couldn't find a similar question. And that is what matters.
oh really? stackoverflow.com/questions/3521487/javascript-body-onclick , stackoverflow.com/questions/9152802/… , stackoverflow.com/questions/12762472/… – yodog Commented Oct 12, 2012 at 16:21onclick
event where as sway doesn't. The 2nd SO link is the best but if you have no idea what onclick is to begin with this question's title is better for the beginner. I wouldn't consider the 1st link related, especially to a beginner. The 3rd is close but still for a beginner is not obvious especially with textarea in the title. – Anthony Hatzopoulos Commented Oct 12, 2012 at 16:53