Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Again with the fields...
Old 06-08-2005, 01:26 PM Again with the fields...
Novice Talker

Posts: 10
Trades: 0
I've been posting some questions mainly about fields lateley and i've gotten good respons.

But theres one more thing I need to learn how to do:

I need to have have two links that add and subtract 1 to the value of a text field.
I'm gonna need to do this many times so a function would be best.

This is what I have:

Script:
var fieldname;
function add(fieldname) {
document.forms[0].fieldname.value++ ;
}

Html:
<input type="text" name="ABC" value="0">
<a href="javascript:add(ABC)">plus</a>

But it just doesnt work.
oreoT is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 06-08-2005, 06:50 PM
Phaedrus's Avatar
Ultra Talker

Posts: 271
Location: CA
Trades: 0
Quote:
Originally Posted by oreoT
I've been posting some questions mainly about fields lateley and i've gotten good respons.

But theres one more thing I need to learn how to do:

I need to have have two links that add and subtract 1 to the value of a text field.
I'm gonna need to do this many times so a function would be best.

This is what I have:

Script:
var fieldname;
function add(fieldname) {
document.forms[0].fieldname.value++ ;
}

Html:
<input type="text" name="ABC" value="0">
<a href="javascript:add(ABC)">plus</a>

But it just doesnt work.
You should give your text box an "id" attribute, so you can use document.getElementById() in the add() function:
Code:
function add(field)
{
    field = document.getElementById(field);
Now you can get the field's value, but since it's a text box, the script will see the value as a string, not a number. That's why this line:
Code:
document.forms[0].fieldname.value++ ;
doesn't work in your original function. You need to use parseInt() to get an integer value from a string:
Code:
function add(field)
{
    field = document.getElementById(field);	
    val = parseInt(field.value);
I'm not really sure why, but the ++ syntax isn't going to work, you need to use +=
Code:
function add(field)
{
    field = document.getElementById(field);	
    val = parseInt(field.value);
    val += 1;
Now that you've read the value, turned it into a number, and added one to it, you just need to set the field to the new value:
Code:
function add(field)
{
    field = document.getElementById(field);	
    val = parseInt(field.value);
    val += 1;
    field.value = val;
}
And the HTML can be something like this:
HTML Code:
<input type="text" name="ABC" id="ABC" value="0"> 
<a href="#" onclick="add('ABC');">+</a> 
<a href="#" onclick="subtract('ABC');">-</a> 
Oh, and when passing "ABC" to the function add(), you need to use quotes around 'ABC' otherwise the script thinks you're passing it an object.
__________________

Please login or register to view this content. Registration is FREE
Phaedrus is offline
Reply With Quote
View Public Profile
 
Old 06-09-2005, 05:36 AM
Novice Talker

Posts: 10
Trades: 0
Thanks, I got it working. I didn't need to set the ID, the name was enough.

Also I was trying to make one field be the sum of all the other fields.
So field A would have the value of field 1,2,3,4,5 and so on.

Is there a good way to do this, in a way that the value of field A automatically updates when there is a change in value in the fields 1,2,3,... ?
oreoT is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Again with the fields...
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.11640 seconds with 12 queries