jQuery snippets I can't live without in Drupal

7 +
Jim Olson's picture

Awesome snippets

Thanks, great stuff. #2 (Auto populating a field, and removing text on focus) is really handy for casual drupal users. Any issues/complications for using that snippet on node forms?

himerus's picture

Populating fields on node forms.

I don't think there would be any issues with using the populate field on a node form, except if the form field being populated never was changed, it would submit the default text applied to it.

If using something like that on an advanced node form interface, it would proabably be good to check on the php side if those default values were what was submitted, and if so, just null out the field, and submit nothing for that field.

That's the only thing I can imagine running into with that one.

Jake Strawn
Drupal Rockstar

Susan MacPhee's picture

Newbie question: Where do you

Newbie question:

Where do you put code snipets? In a js file? How do you reference code to work on above items. Awesome to see you last night! Montreal or bust!

himerus's picture

Usually in a js file included in the theme

Usually, I will include this code in a custom js file included via the .info file in the theme, or using drupal_add_js() in a module.

I have at times put this and other code that was more related to functionality than theming in a module so it could quickly be enabled or disabled.

As far as referencing the correct items, in the example related to the populating fields with default info,

populateElement('#search-box input.form-text, #search-block-form input.form-text', 'type something here to search...');

The custom function populateElement() that is being called is passing standard jQuery selectors, in this case the #search-box input.form-text is selecting the default search form, and the #search-block-form ... is referencing the default search form that is presented in the block manager. It is easy to select any particular field based on a class or ID available for that item. If you had a search field that had a class of "my-search-field", the function would look like:

populateElement('.my-search-field', 'type something here to search...');

Jake Strawn
Drupal Rockstar

igor's picture

great! thank your for

great! thank your for populate js. very helpful!

sivakumar's picture

Thanks for your query

Can i get this for primary menu also

himerus's picture

Which part?

Not sure what you're asking for here. Can you elaborate a little what you are trying to accomplish?!

Jake Strawn
Drupal Rockstar

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <p> <h2> <h3> <h4> <h5> <h6> <img> <p>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <javascript>, <php>, <python>, <ruby>. Beside the tag style "<foo>" it is also possible to use "[foo]". PHP source code can also be enclosed in <?php ... ?> or <% ... %>.
  • Image links with 'rel="lightbox"' in the <a> tag will appear in a Lightbox when clicked on.
  • Twitter-style @usersnames are linked to their Twitter account pages.

More information about formatting options