473,545 Members | 2,715 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Concatenation instead of adition!


On an HTML Page I have:

<input type="hidden" name="p_row" value="1" />

then in script we have:

document.forms. grid.p_row.valu e=document.form s.grid.p_row.va ue+10;

Why does this give me 110 as the result? I can see it's concatenating
the string values but how do I tell it to add the things together?

--

jeremy
Sep 26 '06 #1
14 1286


Jeremy wrote:

Why does this give me 110 as the result? I can see it's concatenating
the string values but how do I tell it to add the things together?
<http://jibbering.com/faq/#FAQ4_21>

--

Martin Honnen
http://JavaScript.FAQTs.com/
Sep 26 '06 #2
In article <45************ ***********@new sspool2.arcor-online.net>,
Martin Honnen says...
>

Jeremy wrote:

Why does this give me 110 as the result? I can see it's concatenating
the string values but how do I tell it to add the things together?

<http://jibbering.com/faq/#FAQ4_21>
Thanks Martin, works OK using one of those approaches - so now it says:

document.forms. grid.p_row.valu e=document.form s.grid.p_row.va lue*1+10;

Seems to work - thanks.

--

jeremy
Sep 26 '06 #3
ASM
Jeremy a écrit :
On an HTML Page I have:

<input type="hidden" name="p_row" value="1" />

then in script we have:

document.forms. grid.p_row.valu e=document.form s.grid.p_row.va ue+10;

Why does this give me 110 as the result?
values in inputs are strings

conversion in a number

string * 1
or
+string
and
probably
Number(string)

= +10 + document.forms. grid.p_row.valu e;

or

= document.forms. grid.p_row.valu e*1 + 10;
Sep 26 '06 #4
Jeremy wrote:
On an HTML Page I have:
<input type="hidden" name="p_row" value="1" />
then in script we have:
document.forms. grid.p_row.valu e=document.form s.grid.p_row.va ue+10;
Why does this give me 110 as the result? I can see it's concatenating
the string values but how do I tell it to add the things together?
http://www.javascripttoolbox.com/bestpractices/#plus

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Sep 26 '06 #5
In article <ef*********@ne ws2.newsguy.com >, Matt Kruse says...
Jeremy wrote:
On an HTML Page I have:
<input type="hidden" name="p_row" value="1" />
then in script we have:
document.forms. grid.p_row.valu e=document.form s.grid.p_row.va ue+10;
Why does this give me 110 as the result? I can see it's concatenating
the string values but how do I tell it to add the things together?

http://www.javascripttoolbox.com/bestpractices/#plus

So I can folow Martin Honnen's suggestion and do this:

document.forms. grid.p_row.valu e=document.form s.grid.p_row.va lue*1+10;

or yours and do this:

document.forms. grid.p_row.valu e=+document.for ms.grid.p_row.v alue+10;
Both appear to work equally well - does it matter which - is one a
"better" method and if so, why?

Thanks!
--

jeremy
Sep 26 '06 #6
Jeremy wrote:

[snip]
So I can folow Martin Honnen's suggestion and do this:

document.forms. grid.p_row.valu e=document.form s.grid.p_row.va lue*1+10;
Martin didn't suggest that, exactly. The FAQ entry he pointed you to
describes no less than six means of performing string to number type
conversion.
or yours and do this:

document.forms. grid.p_row.valu e=+document.for ms.grid.p_row.v alue+10;
One of those methods was unary plus.
Both appear to work equally well - does it matter which - is one a
"better" method and if so, why?
If you'd followed the link to the FAQ notes, you'd have a partial answer
to that. The unary plus operator is the fastest, though it sometimes
better to wrap the expression in parentheses:

(+string)

For explicitness, the Number constructor function called as a function
is another good alternative:

Number(string)

I wouldn't recommend operator trickery like multiplying by 1 or
subtracting zero as neither of these are primarily designed to perform
type conversion, whereas the other two approaches above are.

I recommend that you read the type conversion article[1] in the FAQ notes.

On a different note, there's little need to keep referencing form
controls from the document object. See "Efficient use of Form
Accessors"[2], part of another FAQ notes article.

Mike
[1] <http://www.jibbering.c om/faq/faq_notes/type_convert.ht ml>
[2] <http://www.jibbering.c om/faq/faq_notes/form_access.htm l#faEff>
Sep 26 '06 #7


Jeremy wrote:

So I can folow Martin Honnen's suggestion and do this:

document.forms. grid.p_row.valu e=document.form s.grid.p_row.va lue*1+10;
I pointed you to the FAQ and that does not suggest doing * 1 as the only
way, instead it lists a couple of options you have. And it refers to
<http://www.jibbering.c om/faq/faq_notes/type_convert.ht ml>
for further information.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Sep 26 '06 #8
In article <45************ ***********@new sspool3.arcor-online.net>,
Martin Honnen says...
>

Jeremy wrote:

So I can folow Martin Honnen's suggestion and do this:

document.forms. grid.p_row.valu e=document.form s.grid.p_row.va lue*1+10;

I pointed you to the FAQ and that does not suggest doing * 1 as the only
way, instead it lists a couple of options you have. And it refers to
<http://www.jibbering.c om/faq/faq_notes/type_convert.ht ml>
for further information.
I stand corrected - I took the first solution I saw :) and stopped
reading there.

Thanks
--

jeremy
Sep 26 '06 #9
Jeremy wrote:
In article <45************ ***********@new sspool3.arcor-online.net>,
Martin Honnen says...
>Jeremy wrote:
>>So I can folow Martin Honnen's suggestion and do this:

document.form s.grid.p_row.va lue=document.fo rms.grid.p_row. value*1+10;

I pointed you to the FAQ and that does not suggest doing * 1 as the only
way, instead it lists a couple of options you have. And it refers to
<http://www.jibbering.c om/faq/faq_notes/type_convert.ht ml>
for further information.

I stand corrected - I took the first solution I saw :) and stopped
reading there.
I suppose that is quicker than trying to understand what you are doing.

Richard.

Sep 26 '06 #10

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

Similar topics

5
3625
by: Jonas Galvez | last post by:
Is it true that joining the string elements of a list is faster than concatenating them via the '+' operator? "".join() vs 'a'+'b'+'c' If so, can anyone explain why?
1
14129
by: G Kannan | last post by:
Hey all! I have written a perl script to retrieve information from a HTML Form and insert the data into an Oracle database table. I am gettting the the following error message: "Use of uninitialized value in concatenation (.) at register.pl line 38, <STDIN> line 10." The PERL code is as follows:
7
8021
by: Paul Davis | last post by:
I'd like to overload 'comma' to define a concatenation operator for integer-like classes. I've got some first ideas, but I'd appreciate a sanity check. The concatenation operator needs to so something like this: 1) e = (a, b, c, d); // concatenate a,b,c,d into e 2) (a, b, c, d) = e; // get the bits of e into a,b,c, and d For example,...
2
1816
by: Ducojansen | last post by:
Greetings, I'm using the procedure below to insert records into a table which contains a field (SalesOrder) that must be in the format 8-1 8-2 8-3 8-4
33
4642
by: genc_ymeri | last post by:
Hi over there, Propably this subject is discussed over and over several times. I did google it too but I was a little bit surprised what I read on internet when it comes 'when to use what'. Most of articles I read from different experts and programmers tell me that their "gut feelings" for using stringBuilder instead of string concatenation...
4
2107
by: Cristian.Codorean | last post by:
I was just reading a "Python Speed/Performance Tips" article on the Python wiki http://wiki.python.org/moin/PythonSpeed/PerformanceTips and I got to the part that talks about string concatenation and that it is faster when using join instead of += because of strings being immutable. So I have tried it: from time import time t=time()
34
2625
by: Larry Hastings | last post by:
This is such a long posting that I've broken it out into sections. Note that while developing this patch I discovered a Subtle Bug in CPython, which I have discussed in its own section below. ____________ THE OVERVIEW I don't remember where I picked it up, but I remember reading years ago that the simple, obvious Python approach for...
6
5629
by: oscartheduck | last post by:
Hi folks, I have a little script that sits in a directory of images and, when ran, creates thumbnails of the images. It works fine if I call the function inside the program with something like "thumbnailer("jpg), but I want to use a regular expression instead of a plain string so that I can match jpeg, jpg, JPEG etc.
34
3497
by: raylopez99 | last post by:
StringBuilder better and faster than string for adding many strings. Look at the below. It's amazing how much faster StringBuilder is than string. The last loop below is telling: for adding 200000 strings of 8 char each, string took over 25 minutes while StringBuilder took 40 milliseconds! Can anybody explain such a radical...
0
7499
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...
0
7432
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...
0
7689
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. ...
0
6022
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...
1
5359
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...
0
3470
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1919
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
1
1044
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
743
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...

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.