why doesn't my button click event fire?

E

Eric

I'm new to JavaScript and I wrote this code to play with. Oddly, if I
enter text in a box and then press the button, I only get the onChange
event for the text box and not the button's onclick event. But if I
press the button without entering text first, the button click event
does work. What's up?

<html>
<body>
<h3>Events on Buttons and Text Boxes</h3>

<input id="myTextBox1" type="text" onChange="doChange1()" /> <br />
<input id="myTextBox2" type="text" onChange="doChange2()" /> <br />

<input id="myButton" type="button" onclick="doClick()" value="Click me"
/>

<script type="text/javascript">
function doChange1(e)
{
var val = document.getElementById("myTextBox1").value;
alert("You typed: " + val);
}

function doChange2(e)
{
var val = document.getElementById("myTextBox2").value;
alert("You typed: " + val);
}

function doClick(e)
{
var _num = prompt("Enter a number", "100");
alert("You typed: " + _num); // number converted to string
automatically
}
</script>

</body>
</html>
 
A

ASM

Eric a écrit :
I'm new to JavaScript and I wrote this code to play with. Oddly, if I
enter text in a box and then press the button, I only get the onChange
event for the text box and not the button's onclick event. But if I
press the button without entering text first, the button click event
does work. What's up?

You have to let time to time :

function doChange2(e)
{
var val = document.getElementById("myTextBox2").value;
setTimeout(function(){alert("You typed: " + val);},200);
}

fine for me
but could need a longer timer than 200 miliseconds
(depends of processor's capacity ?)
 
E

Eric

ASM said:
Eric a écrit :

You have to let time to time :

function doChange2(e)
{
var val = document.getElementById("myTextBox2").value;
setTimeout(function(){alert("You typed: " + val);},200);
}

Very good - this fixed it on IE. It seems it did work OK on FireFox
without this but I wasn't able to test it on FireFox earlier.

But why do we need a time delay/timeout here? This doesn't make sense
to me. I could understand a delay if we were executing dynamically
constucted code, or code that wasn't fully downloaded to the browser,
but this is a simple page?

Eric
 
L

Lee

Eric said:
I'm new to JavaScript and I wrote this code to play with. Oddly, if I
enter text in a box and then press the button, I only get the onChange
event for the text box and not the button's onclick event. But if I
press the button without entering text first, the button click event
does work. What's up?

This is why alert() isn't a good way to play with event handlers.

Think about when the onchange event of a text input field happens --
when the mouse is clicked outside the text field or when the input cursor is
tabbed out.

So if you enter text in one of your fields and then click the button, what event
fires first?


--
 
E

Eric

1. I don't see any behavior here that's unexpected. I tested your code
on IE and Firefox and the button click handler gets fired every time I
click it.

It didn't work for me on IE6 or IE7 on WinXP SP2, but it did work on
FireFox when I tested it later. I only had a problem if I entered text
before pressing the button. The timeout suggested by the other poster
fixed it, but I really can't see why?
2. Why do you duplicate the change handler? Much better would be:
<input id="myTextBox1" type="text" onChange="doChange(this)" /> <br />
<input id="myTextBox2" type="text" onChange="doChange(this)" /> <br />

This is indeed better! I didn't know that "this" could be used to pass
the name of the control, but it does make perfect sense that it would.
This could be a big savings in code while making it more maintainable.
Of course, in that case, your handler doesn't get an Event object
passed to it. But you weren't using it anyway. Pretty soon you'll
need to learn about Event objects and unobtrusive event handlers, but
that's too involved for this post. Check out
http://www.quirksmode.org/js/introevents.html for an overview.

Cool site - I may buy that book. He has a clean presentation that is
easy to follow.

Speaking of quirks mode (we weren't really, but I saw it in the URL
above), I've been reading about this but I can't understand how to be
compatible with both old browsers (quirks mode) and new browsers (via
the use of a DOCTYPE) without sprinkling my code with version checks
anyway. If I specify that I'm XHTML complaint, I still have to test for
old browsers and adjust my code accordingly. So is a DOCTYPE really
helping me if I have to use ugly browser testing code anyway?
3. Your script tags should really go inside your head tags. Actually,
your scripts should really go in a separate file. For a simple example
like this it isn't a big deal, but for a larger project it's pretty
important to keep track of your code in this way. Once you start
peppering <script> tags in the body of your HTML you quickly lose the
ability to keep track of what's where.

Thanks for the advice. I understand that script in the head will finish
loading and be available before the UI renders, thereby eliminating
some possible "object expected" errors. And putting it in a separate
file may also allow the browser to cache it.

Right now I'm just making simple test pages so I can learn from it.
I'll do my best to follow the "best practices" in code I deploy.
4. In your onClick handler, you should understand that _num is always a
string (whether it contains digits or not), it's never a number. If
you wanted an actual number you could do math with, you need to call
parseInt(_num).

Thanks for this clarification, also. I'm not used to a typeless
language that really does have types, and I assumed the conversion
would always be automatic. But in my code I wasn't trying to do math on
it, anyway. I guess if I tried to add 1 it would have blown up unless I
had the parseInt.

Eric
 
A

ASM

Eric a écrit :
But why do we need a time delay/timeout here?

Because you can fight the processor and its speed :)

You leave the text field and change fires before you can reach the
button, your eventual click on this last one doesn't take effect : the
browser is already at work krumbling its mind to display the alert.

During displaying alert box all JS is stopped.

Delay perhaps could be shorter if instead to call an alert you innerHTML
message in some P or span.

What you can also do in your text fields is :
onchange="alert('somenthing changed); doChange1(); myButton.click();"
(don't forget to name your button which has only an id)
 
A

ASM

Eric a écrit :
Thanks for this clarification, also. I'm not used to a typeless
language that really does have types, and I assumed the conversion
would always be automatic. But in my code I wasn't trying to do math on
it, anyway. I guess if I tried to add 1 it would have blown up unless I
had the parseInt.

Say a number in a text field :
var n = document.myForm.myTextField.value; // n is text
n = n*1; // number
n = +n; // number
n = Number(n); // number
n = parseInt(n); // number
n = +n + ''; // text
n = Number(n)+''; // text

document.myForm.myTextField.value++; // increase this value by step of 1
// if value was a number
 
E

Eric

David said:
3. Your script tags should really go inside your head tags.

I just tried this and it also fixed the problem with IE.

Maybe it's a good rule of thumb to put all the script code in the head,
or at least in the body before it's referenced by any UI objects.

Eric
 
D

Dr J R Stockton

Thu said:
Not exactly. In JS the following is true:

'1' + '1' == '1' + 1 == '11'

Actually, executed as JS, that gives false .
The + operator is used to concatenate strings, and anything that's not
a string, when added to a string, is automatically converted rather
than throwing an error.

It does, however, convert a Boolean to a Number, and a Function to NaN.

It's a good idea to read the newsgroup and its FAQ. See below.
 
E

Evertjan.

Dr J R Stockton wrote on 18 nov 2006 in comp.lang.javascript:
Actually, executed as JS, that gives false .

True, it does.

Try:

alert( '1' + '1' == '1' + 1 == '11' ) // false, because:

alert( ('1' + '1') == ('1' + 1) == '11' ) // false

alert( '1' + ('1' == '1') + (1 == '11') ) // 1truefalse

// btw:

alert( '1' + 1 === '11' ) // true
 
D

Dr J R Stockton

Sat said:
Try:

alert( '1' + '1' == '1' + 1 == '11' ) // false, because:

I can try it without the alert( or the ) . I just copy from one
window, paste into another, and press "Eval".
 
E

Evertjan.

Dr J R Stockton wrote on 18 nov 2006 in comp.lang.javascript:
I can try it without the alert( or the ) . I just copy from one
window, paste into another, and press "Eval".

I have heard one could press charges, but
how the (d)evil does one press eval?
 
D

Dr J R Stockton

Sat said:
Dr J R Stockton wrote on 18 nov 2006 in comp.lang.javascript:


I have heard one could press charges, but
how the (d)evil does one press eval?

"Eval", not "eval".

The window to be copied into can be left open, and at least partly
visible on the screen, and "Eval" is written on the first button. One
moves the mouse so that ... .

The window in question is one showing a local copy of my Web page
<URL:http://www.merlyn.demon.co.uk/js-quick.htm>.

To see how it works, get that page, press "Demo" 6N+5 times then press
Eval (NewW only works with some Demo numbers). Demos 2, 3, & 6 are
better examples of normal use.

But read the rest of the page too.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top