How to make this code work with onBlur mode in JavaScript - javascript

My code works with 'Onclick'. I need it to work with onBlur mode in JavaScript.
My code
if(!m.isSystemButtonClicked)
{
console.debug("Inside onHide. m.isSystemButtonClicked=",m.isSystemButtonClicked);
if(m.unloadListener)
{
window.onbeforeunload = m.unloadListener;
}
}

onblur Event syntax
In HTML:
<element onblur="myScript">
In JavaScript:
object.onblur=function(){myScript};
In JavaScript, using the addEventListener() method:
object.addEventListener("blur", myScript);
example :
source - http://www.w3schools.com
<!DOCTYPE html>
<html>
<body>
<p>When you enter the input field (child of FORM), a function is triggered which sets the background color to yellow. When you leave the input field, a function is triggered which removes the background color.</p>
<form id="myForm">
<input type="text" id="myInput">
</form>
<script>
var x = document.getElementById("myForm");
x.addEventListener("focus", myFocusFunction, true);
x.addEventListener("blur", myBlurFunction, true);
function myFocusFunction() {
document.getElementById("myInput").style.backgroundColor = "yellow";
}
function myBlurFunction() {
document.getElementById("myInput").style.backgroundColor = "";
}
</script>
</body>
</html>
Your example can be rewritten as
m.onblur = function() {
console.debug("Inside onHide. m.isSystemButtonClicked=",m.isSystemButtonClicked);
if(m.unloadListener)
{
window.onbeforeunload = m.unloadListener;
}
}

Related

I want to disable click property after I click once

**I want to disable clickability after the first click ** I used this code but did not work.
let counter = 0;
inputOptions.forEach((inputOptions) => {
inputOptions.addEventListener("click", () => {
inputOptions.classList.toggle("active");
if (inputOptions.classList.contains("active")) {
inputOptions.style.pointerEvent = 'none';
}
if (inputOptions.innerHTML == "Dhaka") {
counter++;
} else { counter = 0; }
})
})
function getResult() {
document.getElementById("result").innerHTML = "No of Correct answer:" + counter;
}
you can pass parameter { once: true }
document.querySelectorAll('button').forEach((inputOptions) => {
inputOptions.addEventListener("click", (el) => {
console.log(inputOptions)
}, { once: true })
})
<button>A</button>
<button>B</button>
here is sample of code for disabled button after once click.
You need to define id in html then access button like below code.
this is just sample of working code. you need to get idea from this then implement according to your requirement
<html>
<head>
<title>JavaScript - Disable Button after Click using JavaScript Function.</title>
<script type="text/javascript">
function disableButton(btn){
document.getElementById(btn.id).disabled = true;
alert("Button has been disabled.");
}
</script>
</head>
<body style="text-align: center;">
<h1>JavaScript - Disable Button after Click using JavaScript Function.</h1>
<p><input type="button" id="btn1" value="Click to disable button." onclick="disableButton(this)"</p>
</body>
</html>

Stop onclick event, once onblur event got active

I have one text field and one button, I just want to ignore onclick event on button, when onblur events got activated. I tried a few methods but didn't work for me.
<!DOCTYPE html>
<html>
<body>
Enter your name: <input type="text" id="fname" onblur="myFunction(event)">
<button onclick="calldiv(event)">Click</button>
<script>
function myFunction(event) {
// event.preventDefault();
// event.stopPropagation();
// event.stopImmediatePropagation();
console.log("blur");
}
function calldiv(event) {
console.log("div");
}
</script>
</body>
</html>
Result...
1st time button got clicked:-blur
2nd time button got clicked:-div
an easy way to solve it is to make an if statement inside calldiv function
<body>
Enter your name: <input type="text" id="fname" onblur="myFunction(event)" />
<button id="button" onclick="calldiv(event)">Click</button>
<script>
let blured = false;
function calldiv(event) {
console.log(blured);
if (!blured) {
console.log("div");
} else {
blured = false;
}
}
const currentButton = document.getElementById("button");
function myFunction(event) {
console.log("blur");
blured = true;
}
</script>
</body>

Checkbox inserts text into textarea with Javascript

I am trying to do something really simple using only Javascript (not JQuery).
Basically, I want to use a checkbox to toggle the text in a textarea. So if the checkbox is unchecked I want it to say "Hello" in the textarea. If the checkbox is checked, the textarea should say "Goodbye".
I'm just getting started with Javascript, so any help would be appreciated.
Thanks
Here is the code:
var myswitch = document.getElementsByTagName("myonoffswitch");
var mytextarea= document.getElementsByTagName("mytextarea");
myswitch.onchange = function(){
if(this.checked){
mytextarea.value = "Hello"
}else{
mytextarea.value = "Goodbye"
}
}
If your controls are in a form, you can do something really simple like:
<form>
<textarea name="ta"></textarea>
<input type="checkbox" onclick="
this.form.ta.value = this.checked? 'Hello':'Goodbye';
">
</form>
Note that using the change event with a checkbox means that in some browsers, the event won't be dispatched until the checkbox loses focus, so better to use the click event.
You should be using document.getElementById instead of getElementsByTagName
I can't tell from your code snippet if you've wrapped your code in an onload function. This is required in situations where your DOM elements are not loaded in the HTML at the time your javascript is running
Here's an example
window.onload = function () {
var myswitch = document.getElementById("myonoffswitch");
var mytextarea = document.getElementById("mytextarea");
myswitch.onchange = function () {
if (this.checked) {
mytextarea.value = "Hello";
} else {
mytextarea.value = "Goodbye";
}
}
//code here
}
And a fiddle is available here: http://jsfiddle.net/C4jVG/
I've tried something. This should work for you
HTML
<input type="checkbox" id="myonoffswitch">Switch</input>
<textarea id="mytextarea" cols="30" rows="10"></textarea>
Javascript
function fillText() {
var myswitch = document.getElementById("myonoffswitch");
var mytextarea= document.getElementById("mytextarea");
myswitch.onchange = function(){
if(this.checked){
mytextarea.value = "Hello"
}else{
mytextarea.value = "Goodbye"
}
}
}
window.onload = fillText;
Just try replacing getElementsByTagName in your code with getElementById this will solve your problem.

Want to use Javascript and HTML to show button when text entered into text box equals "X"

** Note: I just figured out how to call functions through functions and as such don't really need a lot of help with this. But if there is a cleaner method of doing this. Please let me know!
Okay, so I'm really new to javascript and HTML so don't hate me if I don't understand some of the basics. From what I know this code I've come up with should be working. Anyways, what I want to do is have a blank text box for someone to input text into. When they input text using the onchange command I want that textbox's new value to be the trigger for a button to show up using the "if" statement in Javascript.
For example: If someone inputs into the test box the word "test". When they click outside of the box again I would like a button to show up (what it says is irrelevant). But if they put anything else that isn't equal to the word "test" it won't show a button up.
This is the following code I have.
<html>
<head>
<script>
window.onload=function(){
document.getElementById("button").style.display='none';
}
function textCheck(){
if (document.getElementById("userText").value == "test") alert("hello");
}
</script>
</head>
<body>
<input type="button" id="button" value="New Button"/>
<input type="text" id="userText" onchange="textCheck()"/>
</body>
</html>
I will note that the previous code was done in part with help from other stackoverflow searches. However, this gives me an alert. What I want is a button. So instead of the alert line I need it to run another function to show the button.
Would this following code work?
}
function showButton(){
document.getElementById("button").style.display='block';
}
And if so, how do I replace the alert with code to run a second function? If anyone has any better ideas it would be greatly appreciated.
I think the event you are looking for is onblur not onchange.
The correct implementation would look like this.
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script>
window.onload = function() {
document.getElementById("button").style.display = 'none';
}
function textCheck() {
if (document.getElementById("userText").value == "test") {
alert("incorrect value");
document.getElementById("userText").value = ""; // clearing input field
document.getElementById("userText").focus(); // setting the focus on the input
return false;
} else {
return true;
}
}
</script>
</head>
<body>
<input type="button" id="button" value="New Button"/>
<input type="text" id="userText" onblur="textCheck()" />
</body>
</html>
Hope it helps!!!!
I hope i got what you need and here's the code:
window.onload = function() {
document.getElementById("button").style.display = 'none';
}
function textCheck() {
if (document.getElementById("userText").value == "test") {
//document.getElementById("button").style.display = 'block';
//Alternate
enableButton();
} else {
hideButton();
document.getElementById("userText").value = "";
//This is for clear the text in text field.
}
}
function enableButton() {
document.getElementById("button").style.display = 'block';
}
function hideButton() {
document.getElementById("button").style.display = 'none';
}
Well, you do have all the pieces to get this to work. By putting the code traling the if statement inside curly brackets, you create a code block to be executed when the condition is true.
function textCheck(){
if (document.getElementById("userText").value == "test") {
document.getElementById("button").style.display='block';
}
}
You could try doing something along the lines of this:
var theBtn = document.getElementById("button");
theBtn.addEventListener('click', textCheck, false)
function textCheck(){
if (document.getElementById("userText").value == "test") {
alert("hello");
};
}
Here's a JSFiddle

Detecting change in hidden form field

Im writing a test code to do a counter that stores the value in a hidden form field. Whenever this counter is incremented with a button click, the counter value is stored in the hidden field. I have no problem with this portion.
However, im having problem to display an alert whenever the hidden field is being changed. Pls see my code below and tell me where i have gone wrong. Thank You.
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
<script type="text/javascript">
function startRolling() {
var storage=document.getElementById('store').value;
var tonum;
if(parseInt(storage)==0)
{
tonum=1;
}
else {
tonum=parseInt(storage,10);
}
tonum=tonum+1;
document.getElementById('store').value=tonum;
storage=document.getElementById('store').value;
alert(storage)
}
$(document).ready(function() {
var content = $('#store').val();
$('#store').change(function() {
if ($('#store').val() != content) {
alert('Content has been changed')
}
});
});
</script>
</head>
<body>
<input type="button" id="trigger" value="Start" onclick="startRolling()"/>
<input type="text" id="cnt" readonly="readonly"/>
<input type="hidden" id="store" value="0"/>
</body>
What if you just fire the event without trying to detect the change?
function startRolling() {
var storage=document.getElementById('store').value;
var tonum;
if(parseInt(storage)==0)
{
tonum=1;
}
else {
tonum=parseInt(storage,10);
}
tonum=tonum+1;
document.getElementById('store').value=tonum;
if(storage != tonum) {
alertChange();
}
//storage=document.getElementById('store').value;
//alert(storage)
}
function alertChange() {
alert('Content has been changed');
}
You could also look at the trigger event in jquery: http://api.jquery.com/trigger/.
Try this
function startRolling() {
var storage=document.getElementById('store').value;
var tonum;
if(parseInt(storage)==0)
{
tonum=1;
}
else {
tonum=parseInt(storage,10);
}
tonum=tonum+1;
document.getElementById('store').value=tonum;
//storage=document.getElementById('store').value;
//alert(storage)
}
$(document).ready(function() {
//var content = $('#store').val();
$('#store').change(function() {
//if ($('#store').val() != content) {
alert('Content has been changed')
}
});
Why don't you change the first function to jquery?
From the description of the change-event:
The change event occurs when a control loses the input focus and its value has been modified since gaining focus.
Hidden inputs cannot lose focus(because they never have focus), so change will not fire there anyway.
See Any even to detect when the "Class" attribute is changed for a control for a solution.
Rather than using a change event, I've used a loop event that checks every second.
var content="";
function startRolling() {
var storage=document.getElementById('store').value;
var tonum;
if(parseInt(storage)==0)
{
tonum=1;
}
else {
tonum=parseInt(storage,10);
}
tonum=tonum+1;
document.getElementById('store').value=tonum;
storage=document.getElementById('store').value;
content=storage;
alert(storage)
}
function checkifchanged(){
if ($('#store').val() != content) {
alert('Content has been changed');
}
else{
content = $('#store').val();
}
setTimeout('checkifchanged()',1000);
}
$(document).ready(function() {
content = $('#store').val();
checkifchanged();
});
var content = $('#store').val();
You are storing the changed value and comparing with the same value,
this if statement doesn't execute
if ($('#store').val() != content)
don't store the value just call change event directly.
$(document).ready(function() {
$('#store').change(function() {
alert('Content has been changed')
});

Categories