Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rating: Thread Rating: 2 votes, 2.00 average.
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-22-2010, 12:06 AM   PM User | #1
jmindrup
New to the CF scene

 
Join Date: Jan 2010
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
jmindrup is an unknown quantity at this point
onclick not firing after onchange

I have an input text with an onchange event that calls a function in which an alert box displays. I also have a button whose onclick calls a different function. If the user makes a change in the input text and immediately clicks the button, the onchange event fires, displaying the alert box, but the code in the function for the onclick of the button doesn't execute. I've read that this has something to do with event bubbling, but I haven't seen any solutions. Is there a solution? Is it even possible?
jmindrup is offline   Reply With Quote
Old 01-22-2010, 08:19 AM   PM User | #2
abduraooft
Supreme Master coder!

 
abduraooft's Avatar
 
Join Date: Mar 2007
Location: N/A
Posts: 14,680
Thanks: 158
Thanked 2,182 Times in 2,169 Posts
abduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really nice
Quote:
Is there a solution? Is it even possible?
Posting your code may speak a lot more than what you've described.

PS: Please use [CODE][/CODE] tags to wrap your code while posting here.
__________________
Quote:
The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

Last edited by abduraooft; 01-22-2010 at 09:01 AM..
abduraooft is offline   Reply With Quote
Old 01-22-2010, 08:44 AM   PM User | #3
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,036
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by jmindrup View Post
Is there a solution? Is it even possible?
Well, I expect there is.

You could disable the button while the onchange alert is being shown.

Code:

<input type = "text" onchange = "showAlert1()">
<input type = "button" id = "al2" value = "Click Here" onclick = "showAlert2()">

<script type = "text/javascript">

function showAlert1() {
document.getElementById("al2").value = "Disabled Right Now";
document.getElementById("al2").disabled= true;
alert ("ONE");
document.getElementById("al2").value = "Click Here";
document.getElementById("al2").disabled= false;
}

function showAlert2() {
alert ("TWO");
}
</script>
"Gordon Brown Pledges To Help Flood Devastated Cumbria" - Daily Telegraph

Last edited by Philip M; 01-22-2010 at 03:58 PM..
Philip M is offline   Reply With Quote
Old 01-22-2010, 02:29 PM   PM User | #4
jmindrup
New to the CF scene

 
Join Date: Jan 2010
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
jmindrup is an unknown quantity at this point
I have a textbox and a button on a web page. Im validating the value of text box on onchange event , and again when i click the button.The problem is that when i enter a value in the textbox and then directly click on submit button only the textbox's onchange event is occurring and the onclick event of the button is not getting fired.

Code:
<html>
</head>
<script language="javascript">
function validateMyText(){
alert("validating");
}
function buttonClicked(){
alert("in buttonClicked");
validateMyText();
}
</script>
</head>
<body>
<form>
<input type="text" name="myText" id="myText" 
onchange="validateMyText();"/> 
<input type="button" name="myButton"
id="myButton" onclick="buttonClicked();"/>
</form>
</body>
</html>
I understand that the alert box in the onchange handler prevents the onclick event from bubbling. Is there a workaround, or can this not be done?
jmindrup is offline   Reply With Quote
Old 01-22-2010, 02:34 PM   PM User | #5
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,036
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Have you bothered to read Post #3?
Philip M is offline   Reply With Quote
Old 01-22-2010, 03:44 PM   PM User | #6
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
The logical structure is ... illogical. Why do you need to validate an input twice, first onchange, next onclick something else?
__________________
KOR
Offshore programming
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Kor is offline   Reply With Quote
Old 01-22-2010, 03:47 PM   PM User | #7
jmindrup
New to the CF scene

 
Join Date: Jan 2010
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
jmindrup is an unknown quantity at this point
Philip, thanks for taking the time to respond to my question. When I initially saw your suggestion, I got the idea that maybe you weren't sure what my issue was, especially since someone else responded and asked to see my code. After posting my code and seeing your last response, I figured I would give your solution a try. It does not work. The onclick event handler showAlert2() doesn't fire if a change is made to the input value and user immediately clicks the button. thanks.
jmindrup is offline   Reply With Quote
Old 01-22-2010, 04:03 PM   PM User | #8
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,036
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Well, I do not understand what you are trying to do. However, I find that I did leave out part of the code in Post #3 - sorry, now corrected.

When I enter a value in the box, as soon as the box loses focus alert ONE is triggered, and the button is disabled. Once the alert is dismissed the button is re-enabled, and if clicked alert TWO shows. Surely you do not want BOTH alerts to show? If you do, simply combine the two messages.

As Kor says, "Why do you need to validate an input twice, first onchange, next onclick something else?"

At its simplest, you may not have two modal dialog boxes active at the same time.

Last edited by Philip M; 01-22-2010 at 04:12 PM..
Philip M is offline   Reply With Quote
Old 01-22-2010, 05:19 PM   PM User | #9
jmindrup
New to the CF scene

 
Join Date: Jan 2010
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
jmindrup is an unknown quantity at this point
Philip, I've basically cut and pasted your code into my page and the alert box from the onclick event handler doesn't display. Not sure if maybe it's a browser issue?

I gave a simplified example of what I need to do. I'm not really doing validation in both event handlers. I don't actually need an alert box to display in both the onchange and onclick event handlers. The alert box might display in the onchange event, if the input fails validation. My real problem is that I have some code in the onclick event which needs to execute every time the button is clicked. The code in the onclick event doesn't involve displaying an alert box, but instead does some basic things like change values on other components. Again, the real issue here is that the onclick event handler code does not execute if the alert box is displayed when the onchange event handler fires first.
jmindrup is offline   Reply With Quote
Old 01-22-2010, 05:50 PM   PM User | #10
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,036
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Well, it works for me as I have described in IE and in Firefox. In fact, I do not see how it can not work. In short, the idea is to disable the button while the onchange script is functioning. If that will not do, then I am afraid that I am right out of ideas. Abduraooft?
Philip M is offline   Reply With Quote
Old 01-22-2010, 06:49 PM   PM User | #11
jmindrup
New to the CF scene

 
Join Date: Jan 2010
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
jmindrup is an unknown quantity at this point
Phil, I literally cut and pasted your code into a one page .html file and when i perform the steps in this order, the onclick event doesn't fire. in other words the alert from the onclick event handler never displays. Here's the steps I'm performing:

Enter a letter into the input. Without tabbing out, immediately click the button. I see the alert box from the onchange event handler, but the alert box in the onclick event handler never displays. I suppose I'll have to look at another way of doing what I'm trying to do. Thanks again for your help.
jmindrup is offline   Reply With Quote
Old 01-22-2010, 08:08 PM   PM User | #12
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,036
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by jmindrup View Post
Enter a letter into the input. Without tabbing out, immediately click the button. I see the alert box from the onchange event handler, but the alert box in the onclick event handler never displays.
The button is (temporarily) disabled by the onchange event. So the alert "TWO" will only show when the button is re-enabled and clicked.
Philip M is offline   Reply With Quote
Old 01-22-2010, 09:18 PM   PM User | #13
jmindrup
New to the CF scene

 
Join Date: Jan 2010
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
jmindrup is an unknown quantity at this point
gotcha. It does sound like maybe I didn't explain the scenario well enough. I need for both the onchange and onclick events to fire one right after the other with the steps I described, even if an alert box is displayed in the onchange event.
jmindrup is offline   Reply With Quote
Old 01-23-2010, 07:57 AM   PM User | #14
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,036
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by jmindrup View Post
gotcha. It does sound like maybe I didn't explain the scenario well enough. I need for both the onchange and onclick events to fire one right after the other with the steps I described, even if an alert box is displayed in the onchange event.
Not possible. To recap, you may not have two modal dialog boxes active at the same time.
Philip M is offline   Reply With Quote
Old 01-23-2010, 07:59 AM   PM User | #15
godofreality
Regular Coder

 
godofreality's Avatar
 
Join Date: Jan 2009
Posts: 230
Thanks: 1
Thanked 15 Times in 15 Posts
godofreality can only hope to improve
not to mention if an alert box comes up it disrupts all scripts attempting to execute until the needed action is met in the case closing the alert box
__________________
woot found the avatar options...i swear they didn't exist b4
godofreality is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:02 PM.


Advertisement
Log in to turn off these ads.