473,480 Members | 1,992 Online
Bytes | Software Development & Data Engineering Community
Create 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.value=document.forms.gri d.p_row.vaue+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 1282


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***********************@newsspool2.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.value=document.forms.gri d.p_row.value*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.value=document.forms.gri d.p_row.vaue+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.value;

or

= document.forms.grid.p_row.value*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.value=document.forms.gri d.p_row.vaue+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*********@news2.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.value=document.forms.gri d.p_row.vaue+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.value=document.forms.gri d.p_row.value*1+10;

or yours and do this:

document.forms.grid.p_row.value=+document.forms.gr id.p_row.value+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.value=document.forms.gri d.p_row.value*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.value=+document.forms.gr id.p_row.value+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.com/faq/faq_notes/type_convert.html>
[2] <http://www.jibbering.com/faq/faq_notes/form_access.html#faEff>
Sep 26 '06 #7


Jeremy wrote:

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

document.forms.grid.p_row.value=document.forms.gri d.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.com/faq/faq_notes/type_convert.html>
for further information.

--

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

Jeremy wrote:

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

document.forms.grid.p_row.value=document.forms.gri d.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.com/faq/faq_notes/type_convert.html>
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***********************@newsspool3.arcor-online.net>,
Martin Honnen says...
>Jeremy wrote:
>>So I can folow Martin Honnen's suggestion and do this:

document.forms.grid.p_row.value=document.forms.g rid.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.com/faq/faq_notes/type_convert.html>
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
In article <11**********************@e3g2000cwe.googlegroups. com>,
Richard Cornford says...
Jeremy wrote:
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.
<ouch>

Well I understood enough that we had to tell javascript that these two
variables had to be treated as numbers. The first method satisfied this
need.

--

jeremy
Sep 26 '06 #11
JRS: In article <45***********************@news.orange.fr>, dated Tue,
26 Sep 2006 17:26:24 remote, seen in news:comp.lang.javascript, ASM
<st*********************@wanadoo.fr.invalidposte d :
>
= +10 + document.forms.grid.p_row.value;
Concatenates. Try

= 10 + +document.forms.grid.p_row.value
= +document.forms.grid.p_row.value + 10

N.B. + + != ++ .

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk DOS 3.3, 6.20; Win98. ©
Web <URL:http://www.merlyn.demon.co.uk/- FAQqish topics, acronyms & links.
PAS EXE TXT ZIP via <URL:http://www.merlyn.demon.co.uk/programs/00index.htm>
My DOS <URL:http://www.merlyn.demon.co.uk/batfiles.htm- also batprogs.htm.
Sep 26 '06 #12
document.forms.grid.p_row.value=parseInt(document. forms.grid.p_row.vaue)+10;
will be OK.

Sep 27 '06 #13
JRS: In article <MP************************@news.individual.net> , dated
Tue, 26 Sep 2006 15:59:34 remote, seen in news:comp.lang.javascript,
Jeremy <je********@gmail.composted :
>document.forms.grid.p_row.value=document.forms.gr id.p_row.vaue+10;

Why does this give me 110 as the result?
FAQ.
>I can see it's concatenating
the string values but how do I tell it to add the things together?
document.forms.grid.p_row.value -= -10
// & document.forms.grid.p_row.value -=- 10 // :-(

work for me.

It's a good idea to read the newsgroup and its FAQ. See below.
--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Sep 28 '06 #14
Dr John Stockton wrote:
<snip>
document.forms.grid.p_row.value -= -10
// & document.forms.grid.p_row.value -=- 10 // :-(

work for me.
But what is your point? That the second should not work? It may not be a
particularly clear formatting of source code for humans but it is
unambiguous to tokenise, so the interpreter will not have a problem
seeing the two operators for what they are.

Remember that in ECMAScritp numeric literals can only be positive (or
zero) and the unary negation operator in, for example, -10 is applied at
runtime (though 'optimising' such an occurrence in source code into a
negative numeric value prior to execution of any code in which it appears
would be possible, and would have no consequences).

Richard.
Sep 28 '06 #15

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

Similar topics

5
3614
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
14121
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...
7
8015
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...
2
1806
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
4632
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...
4
2104
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...
34
2613
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. ...
6
5621
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...
34
3487
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...
0
6912
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...
0
7092
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
6981
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5348
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,...
1
4790
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...
0
4488
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3000
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2989
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1304
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 ...

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.