...

View Full Version : Sync with Javascript question



JMJdev
11-19-2010, 10:15 AM
Hi,

I have a form with a input field(name) and a textarea(keywords).
I am using the below code to sync the value of name with keywords.
If additional keywords are added in the textarea and then the name field is altered all additional keywords are lost as they are overwritten by the name field sync.
I would like to place a comma after the synced value and have whatever follows the comma to not be touched.
Like this:

Name: keyword1
Keywords: keyword1, keyword2, keyword3, etc.

Could anyone point me in the right direction?
Any help with this would be most appreciated.


var obj=document.getElementById('name');
var att = {"onkeyup":"sync()"};
for(var n in att) {
obj.setAttribute(n, att[n]);
}
function sync()
{
var v1 = document.getElementById('name');
var v2 = document.getElementById('keywords');
v2.value = v1.value;
}

glenngv
11-19-2010, 11:16 AM
var obj=document.getElementById('name');
obj.onkeyup = sync;

function sync()
{
var v1 = document.getElementById('name');
var v2 = document.getElementById('keywords');
v2.value = (v2.value) ? ', ' + v1.value : v1.value;
}
The way you attach the onkeyup handler is not correct. Event handlers are not strings (as you were doing) but function pointers.

Philip M
11-19-2010, 11:32 AM
'keywords' would seem to be a reserved word in IE, as it does not work. It does work if you change it to something else like 'keywds'.

I am not sure that I understand what is wanted. But erasing the textbox erases the entire textarea.

JMJdev
11-20-2010, 12:09 PM
Thanks for those tips. Not exactly what I am trying to achieve. Just to shed some more light on what I am after.

The name field will be a user inputted value. I would like the name field value to be put at the very start of the keywords text area, trailed by a comma and space.

For example:
Someone enters "My Name is Bob!" into the name field.
"My name is Bob!" is then placed at the very start of the Keywords textarea with a trailing comma and space, like this. "My name is Bob!, ".
Then someone adds to the keywords textarea "flower shop" so it looks like "My name is Bob!, flower shop".

The above example is not my issue. What I am after is when someone then alters the name field from "My Name is Bob!" to "Bob's Flower Shop" I would like that to change in the first comma seperated value of the Keywords textarea.

Any ideas?

Philip M
11-20-2010, 01:10 PM
What do you mean by "someone"? Are you envisaging a number of different users? If so you cannot use Javascript which is purely a client-side language.

JMJdev
11-20-2010, 01:45 PM
Someone, anyone. Whom ever is using it. Lets call him Bob. I understand it is client side.

Philip M
11-20-2010, 03:15 PM
Well, what is the point of someone entering "My Name is Bob!" and "Flower Shop" into a textbox that only that user can see?

JMJdev
11-20-2010, 11:19 PM
Those values are going to be saved to a database and then output somewhere else. I just was using those as an example. It could be anything. Why does there have to be a point? What is with this line of questioning? It is irrelevant.

JMJdev
11-21-2010, 02:30 AM
I worked it out.


function sync()
{
var v1 = document.getElementById('name');
var v2 = document.getElementById('keywords');
var valofv2 = v2.value;
var splitv2 = valofv2.split(",");
splitv2[0] = v1.value;
v2.value = splitv2;
}

The only problem left is if I add a comma in the name field it screws the whole thing up.

glenngv
11-22-2010, 08:29 AM
Remove the comma


splitv2[0] = v1.value.replace(/\,/g, "");

JMJdev
11-23-2010, 01:08 AM
Remove the comma


splitv2[0] = v1.value.replace(/\,/g, "");



Works like a charm. Thanks Glenn!

JMJdev
11-26-2010, 02:01 AM
Adding onto the current function I would like the name field to sync with the the correct split of the keywords textarea even if it is not in position 1.

This is what I have tried but I am new to this and it is not working.


function sync()
{
var v1 = document.getElementById('name');
var v2 = document.getElementById('keywords');
var valv1 = v1.value;
var valv2 = v2.value;
var splitv2 = valv2.split(",");

for(i = 0; i < splitv2.length; i++){
if(splitv2[i] == valv1) {
splitv2[i] = v1.value.replace(/\,/g, "");
}

}
v2.value = splitv2;
}

Thanks



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum