I want a click on #parent to toggleClass a 'newclass', on the parent's child element, 'child'. The page has an AJAX call, so I need the function to only apply on page load.

However, I can only get the 'newclass' to begin to toggle on the last AJAX call, I'm not sure why. Before the last AJAX call, clicking #parent has no effect.

Also, #parent is not unique, hence the $(this). (I only want it to toggle the class for the .child that had it's #parent clicked).

Any ideas why this won't work?

<div id="#parent">
  <div class="child">
  Content #1
<div id="#parent">
  <div class="child">
  Content #2

(function ($) {
Drupal.behaviors.arbitrarystring = {
attach:function(context, settings) {
$('#parent').once('arbitrarytitle', function() {
$('#parent').click(function() {


Tried setting up a JS fiddle, but was having problems to emulate the AJAX calls.

Thanks everyone!

EDIT: Here is the fiddle, without AJAX Request or the Drupal.behaviors wrapper; I was getting a syntax error when trying to close the brackets.


  • Are you using Drupal Forms API and #ajax key in it?
    – FriOne
    Nov 28 '14 at 7:59
  • Fiddle posted. Thanks! @FriOne I simply want to show some fields as they relate to an image, so upon clicking the image the fields will hover over it. No forms included; hope this is what you meant! Thanks!
    – nikk wong
    Nov 28 '14 at 12:47

You should have unique id's (#id-name) and as many elements as you would like sharing the same class (.class-name). I mean, you should not have multiple elements with same id inside HTML. Also, you cannot have [#] or [.] signs inside HTML. They should appear only inside CSS and jQuery code.

Here you can find the solution:



Not the answer you're looking for? Browse other questions tagged or ask your own question.