Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Other Forums
 Other Topics
 JavaScript: On check. Append the value

Author  Topic 

afrika
Master Smack Fu Yak Hacker

2706 Posts

Posted - 2006-01-30 : 06:20:16
Hello,
i have this script below:
Its a function that writes the value of the checkbox to the textarea.

However, if there was an exisiting value there before, it removes it before writing the new value.

How do i preserve the existing value, before writing or appending the new value of the checkbox

thank you, Afrika

[CODE]
<script>
function boxClicked(o) {
var phone_nu = "", count = 0;
var form = o.form;
// use the checkbox name from html above
var boxes = form["box"];
for(var i = 0; i < boxes.length; i++) {
if(boxes[i].checked) {
phone_nu += boxes[i].value;
if (i != boxes.length - 1)
phone_nu += ",";

count++;
}
}



parent.document.getElementById('phone_nu').value = phone_nu.substr(0, phone_nu.length - 1);

parent.document.getElementById('count').value = count;
}
</script>
[/CODE]

shallu1_gupta
Constraint Violating Yak Guru

394 Posts

Posted - 2006-01-30 : 07:04:47
Do you want to append the existing value to the new value ?

parent.document.getElementById('phone_nu').value + = phone_nu.substr(0, phone_nu.length - 1);

Go to Top of Page

afrika
Master Smack Fu Yak Hacker

2706 Posts

Posted - 2006-01-30 : 08:07:46
Many thanks Shallu1_gupta

Yes, i would like to append the exisiting value to the new. However, the above changes with the plus sign dont work +

Any advice ?
Go to Top of Page

shallu1_gupta
Constraint Violating Yak Guru

394 Posts

Posted - 2006-01-30 : 23:13:11
Is it giving any error?
try putting it in some variable then appending that with the current value.
var existing_val = parent.document.getElementById('phone_nu').value;

parent.document.getElementById('phone_nu').value = existing_val + phone_nu.substr(0, phone_nu.length - 1);
Go to Top of Page

afrika
Master Smack Fu Yak Hacker

2706 Posts

Posted - 2006-02-03 : 12:52:26
Sorry for getting back this late,

The initial script has two errors

parent.document.getElementById('phone_nu').value + = phone_nu.substr(0, phone_nu.length - 1);


One, it does append the checkbox value to the exisiting value, but
1. It does not add a comma
2. IF there was no exisiting value, and i use the checkbox, it writes the new value, but if i uncheck it, it does not remove it, and also it appends another duplicate value to it. e.g.

if i type "hello" and then use the checkbox and the value is "afrika" it appends "helloafrika"

if i uncheck it, the value remains, and if i re-check it, it appends "helloafrikaafrika" etc

The script was fine, as it is above, in the first post.

Any advice???

Thanks
Go to Top of Page
   

- Advertisement -