Hello,
I am trying to work out a way to copy the value of a form element into another form element on submit.
Background:
I've written a small module essentially adding an autocompletion lookup form field for profile values. This is quite handy as it allows for a 'Lastname' autocompletion field (The lastname field has been added as a cck field used by the profile module) .
So far so good.
My autocompletion widget happily perfoms the profile lookup and returns all required values from the db. It does all the generic things, e.g. it adds the key of the $matches array() as the form field value etc..
In order to be flexible and also because it's very convenient I have implemented that autocompletion field widget as a block. The block only appears on the og/invite/* page and has a submit button.
Now, what I am trying to achieve is to copy the selected value of the autocompletion field on submit/ on click to another form field element on the same page, the og invite form, that is:
The value of ' name="title" '
...into name="mails"
I figured JavaScript or Ajax would be nice. But I can't seem to get that going in Drupal.
Your thoughts please.
Kind regards
Comments
If you are writing your own
If you are writing your own module, include a submit hook for this form. In there you can do whatever you want with the form elements.
Regards
Werner
Thanks. Yes I did that
Thanks. Yes I did that already. But this is not exactly what I need.
I need to copy a value without submitting the form or perhaps loading another page(client side). Initially when I wrote this post I figured I can find a way to attach a JavaScript function to the submit event without actually performing a submit. Now I am trying to achieve that using a generic link.
Here's the corresponding link:
$form['void'] = array('#value' => l('Test', "JavaScript:void(0);", array('class' => 'add-link')));Does that make sense?
Ok, here's what I got and it does the job so far
Whenever the link with the class
add-linkis clicked the value<value of my form field>is being copied into another textarea.That all works fine. Clicking
Testcopies the value but it also still performs a GoTo(http://www.<site_name>/JavaScript:void(0);)How can I prevent that from happening?
As you can see I tried
JavaScript:void(0);. But that string is only being appended to the URL. I would need something which does not actually submit or perhaps something that does not GoTo (load a page)Any idea
Sorry, I haven't dealt with
Sorry, I haven't dealt with javascript in drupal so far, but why shouldn't it be possible to execute the transfer function in javascript with a double-click on the textfield or by changing the focus to the next field. It is feasable in javascript, that I know for shure, with on-conditions.
Regards
Werner
populate one field with the value of another
I needed to populate the URL path field with the value of the node title (w/o the use of PathAuto.)
-------------------------------------------------------
"The sting in any rebuke is the truth." - Benjamin Franklin
to prevent the goto take
to prevent the goto take action when u click the "a", add this
event.preventDefault();
at the end of your click function.
i.e.
click(function() {
....;
event.preventDefault();
})