473,762 Members | 7,330 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

why doesn't my button click event fire?

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="doCha nge1()" /<br />
<input id="myTextBox2 " type="text" onChange="doCha nge2()" /<br />

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

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

function doChange2(e)
{
var val = document.getEle mentById("myTex tBox2").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>

Nov 16 '06
17 9680
ASM said the following on 11/16/2006 9:11 PM:
Eric a écrit :
>David Golightly wrote:
>>Eric wrote:
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.

Say a number in a text field :
var n = document.myForm .myTextField.va lue; // n is text
n = n*1; // number
n = +n; // number
n = Number(n); // number
n = parseInt(n); // number
var n = "09";
k = parseInt(n);
alert(k); //0

Don't use parseInt without the Radix unless you know for absolute
certainty that it doesn't contain a leading 0 or 0x

<URL: http://jibbering.com/faq/#FAQ4_12>

--
Randy
Chance Favors The Prepared Mind
comp.lang.javas cript FAQ - http://jibbering.com/faq
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Nov 17 '06 #11
In message <11************ **********@h48g 2000cwc.googleg roups.com>, Thu,
16 Nov 2006 18:26:26, David Golightly <da******@gmail .comwrites
>
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.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v6.05 IE 6
<URL:http://www.jibbering.c om/faq/ Old RC FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Nov 17 '06 #12
Dr J R Stockton wrote on 18 nov 2006 in comp.lang.javas cript:
>>'1' + '1' == '1' + 1 == '11'

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

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Nov 18 '06 #13
In message <Xn************ *******@194.109 .133.242>, Sat, 18 Nov 2006
10:40:53, Evertjan. <ex************ **@interxnl.net writes
>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".

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v6.05 IE 6
<URL:http://www.jibbering.c om/faq/ Old RC FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Nov 18 '06 #14
Dr J R Stockton wrote on 18 nov 2006 in comp.lang.javas cript:
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?

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Nov 18 '06 #15
<html>
<body>
<textarea id="jscode">
</textarea>
<input type="button"
onclick="alert( eval(document.g etElementById(' jscode').value) )"
value="Eval" />
</body>
</html>

that's how.

Nov 19 '06 #16
David Golightly wrote on 19 nov 2006 in comp.lang.javas cript:
that's how.
That's how what, David?

Please quote on usenet, this is not email.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Nov 19 '06 #17
In message <Xn************ *******@194.109 .133.242>, Sat, 18 Nov 2006
23:39:31, Evertjan. <ex************ **@interxnl.net writes
>Dr J R Stockton wrote on 18 nov 2006 in comp.lang.javas cript:
>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?
"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.demo n.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.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v6.05 IE 6
<URL:http://www.jibbering.c om/faq/ Old RC FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Nov 19 '06 #18

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
3012
by: Andrés Giraldo | last post by:
Hi! I'm adding an asp button to a datagrid on the ItemDataBound event, when the user clicks on this button, I basically remove the button and create other 2 buttons... my problem is.. the 2 last buttons doesn't fire his events... what I'm doing wrong? Thanks!
5
6829
by: Carlo Marchesoni | last post by:
From an aspx page (A.aspx) I open another one (B.aspx - for table lookup). When the user selects an entry in B.aspx I would like to force a button's event in A.aspx to be fired. I guess the only way is using javascript - does anybody have a sample for this ? Thanks
2
2355
by: Liqun Xu | last post by:
Hallo NG, I created a Button with Click-Event dynamically: System.Web.UI.WebControls.Button bt_1 = new Button(); bt_1.Click += new EventHandler(bt_1_click); and I implemented the Funktion bt_1_click in which I created a second Button dynamically too. System.Web.UI.WebControls.Button bt_2= new Button();
2
3896
by: Sam Miller | last post by:
Hi, I have a button event that won't fire. I left it on Friday and it worked fine. I came back in on Monday and it won't fire. I tried putting another button and just putting a response.write on its click handler but it won't fire either telling me no button click events will fire. But my calendar control on the same page does fire. In the debugger I put a break point in the calendar event and the button
1
1350
by: John | last post by:
Hi, I have an asp.net page with some validation. On one machine the button click event works fine, whilst on another the button click event does not fire at all Any suggestions welcome! Thanks Mark
24
7701
by: Charles Law | last post by:
When I click a button I don't want the click event to fire. Is this possible? In fact, what I would really like is to be able to intercept the click event, perform some action, and then prevent the click from passing through to the button. Any help or suggestions much appreciated. Charles
2
2394
by: Chu | last post by:
Thanks everyone for taking a moment to read this. I've got a page where I use a LinkButton and I wire up a dynamic event to the button. When the user clicks the button, the event is fired as expected. In the event code for that button, a new LinkButton is added to the page and is wired up to yet a different event, however when clicked, the page is posted back but the event is not triggered. I'm assuming it has something to do with the...
6
3360
by: Jon Paal | last post by:
validation doesn't fire what's missing ????? /////// ---- code -----/////////////////////////// Sub btnSubmit_Click(sender As Object, e As System.Web.UI.ImageClickEventArgs) 'Handles btnSubmit.Click If page.isValid then ..... end if
5
5893
by: Tony | last post by:
I am continuing to develop an Access 2007 application which was originally converted from Access 2003. In Access 2003 I was able to disable the Access Close button in the top righthand corner of the screen. I have been unable to find any way to disable this button in Access 2007 and subsequently I have been forced to find ways to detect and handle the situations after the Access Close button has been clicked. I have been largely...
0
9554
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9378
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10137
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9927
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8814
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7360
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
3914
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3510
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2788
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.