using jquery to access a form element
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.
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@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/
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@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@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/
Actually, if the same form is used more than once on a page, you can almost guarantee that it will *not* have the same name. Take a look at the comments in "form_clean_id()" concerning its effects on jQuery. I think adding a class might make this more difficult, unless that code is fully aware of multiple instances of a form on a page. Nancy E. Wichmann, PMP Injustice anywhere is a threat to justice everywhere. -- Dr. Martin L. King, Jr. ________________________________ From: Steven Jones 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
I was hoping to find a way to tell my js code which id would have the form element. I inserted my js code inside the form() 2010/6/23 nan wich <nan_wich@bellsouth.net>:
Actually, if the same form is used more than once on a page, you can almost guarantee that it will *not* have the same name. Take a look at the comments in "form_clean_id()" concerning its effects on jQuery. I think adding a class might make this more difficult, unless that code is fully aware of multiple instances of a form on a page.
Nancy E. Wichmann, PMP
Injustice anywhere is a threat to justice everywhere. -- Dr. Martin L. King, Jr.
________________________________ From: Steven Jones
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
-- *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.
You can give your element a name you know won't be used by anything else, then access it like this: module: $form['mywierdelementname']=array(....); JS: $("input[name=myweirdelementname]") If you want the form id that element is in, then: $("input[name=myweirdelementname]").parent("form").attr("id") But the best (and more or less Drupal) way is to assign a class to that form element and attach any behaviors through the Drupal JS api: MODULE: $form['myelement']=array('#attributes'=>array('class'=>'myclassname'), ...); JS; Drupal.behaviors.mybehaviourname = function(context) { $('.myclassname:not(.myclassname-processed)', context).each(function() { $(this).addClass('myclassname-processed'); // Attach whatever you want to your input element now using $(this) }); }; Put that in a JS file and load it on your page with drupal_add_js and it will automatically fire when Drupal does all the other Javascript bindings. Also, if you ever change things down the road (say add AJAX) and that element gets reloaded, it will automatically reattach the behaviors to it. Jamie Holly http://www.intoxination.net http://www.hollyit.net On 6/23/2010 8:45 AM, Lluís Forns wrote:
I was hoping to find a way to tell my js code which id would have the form element.
I inserted my js code inside the form()
2010/6/23 nan wich<nan_wich@bellsouth.net>:
Actually, if the same form is used more than once on a page, you can almost guarantee that it will *not* have the same name. Take a look at the comments in "form_clean_id()" concerning its effects on jQuery. I think adding a class might make this more difficult, unless that code is fully aware of multiple instances of a form on a page.
Nancy E. Wichmann, PMP
Injustice anywhere is a threat to justice everywhere. -- Dr. Martin L. King, Jr.
________________________________ From: Steven Jones
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
participants (5)
-
icerain -
Jamie Holly -
Lluís Forns -
nan wich -
Steven Jones