[development] using jquery to access a form element

Steven Jones steven.jones at computerminds.co.uk
Wed Jun 23 09:02:06 UTC 2010


However, you cannot therefore guarantee that the form element will
always have the same ID.

Usually I'd add a class to the item I want to add the behaviour to (in
form alter or theme function), and then attach the javascript using
the Drupal.behaviors technique.

Regards
Steven Jones
ComputerMinds ltd - Perfect Drupal Websites

Phone : 024 7666 7277
Mobile : 07702 131 576
Twitter : darthsteven
http://www.computerminds.co.uk



On 23 June 2010 09:54, icerain <doubaokun at gmail.com> wrote:
> Drupal will not create the elements with the same ID. So do in this way is OK.
>
> On Wed, Jun 23, 2010 at 4:34 PM, Lluís Forns <enboig at gmail.com> wrote:
>> I am adding some jQuery to my form to update a field value. It works
>> but I think there should be a better way to select a field, I use
>> "$('#...')" but I am worried it will not work if form is shown twice
>> in a page. Any advice?
>>
>> drupal_add_js("$(document).ready(function() {
>>  $('#edit-data-assentament-popup-datepicker-popup-0').change(function () {
>>    $('#edit-data-paper-popup-datepicker-popup-0').attr('value',
>> $('#edit-data-assentament-popup-datepicker-popup-0').attr('value'));
>>  });
>>   $('#edit-data-paper-popup-datepicker-popup-0').blur(function () {
>>     $('#edit-data-paper-popup-datepicker-popup-0').attr('value',
>> $('#edit-data-assentament-popup-datepicker-popup-0').attr('value'));
>>     $(this).unbind('blur');
>>     $('#edit-data-assentament-popup-datepicker-popup-0').unbind('change');
>>  });
>> });", 'inline');
>>
>> thanks
>>
>> --
>> *Les normes hi són perquè hi pensis abans de saltar-te-les
>> *La vida és com una taronja, què esperes a exprimir-la?
>> *Si creus que l'educació és cara, prova la ignorància.
>> *La vida és com una moneda, la pots gastar en el que vulguis però
>> només una vegada.
>> *Abans d'imprimir aquest missatge, pensa en el medi ambient.
>>
>
>
>
> --
> A decathlon Drupal developer & programmer
> http://blog.eood.cn/
>


More information about the development mailing list