Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

jQuery Selector

 
Ranch Hand
Posts: 541
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's the code https://jsfiddle.net/gr55jpaq/1/

I want to add onClick function to each <a> tag of div that has 'moviePosterDiv' class. I am new to jQuery.
 
Bartender
Posts: 1868
81
Android IntelliJ IDE MySQL Database Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can find a version which can be used as starting point here https://jsfiddle.net/gr55jpaq/23/#

The short version is this seems to work:

With this content:

I did not use your content as I wanted to keep it simple with the basics.
 
Pete Letkeman
Bartender
Posts: 1868
81
Android IntelliJ IDE MySQL Database Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just a few quick notes Saurabh on what I did:
  • I changed it from the ID selector to the class selector. I suppose either will do in this case.
  • I did a quick Google search to find an example of how to add the on click event. I always have to do this as I do not recall how to do this.
  • Then I fiddled with the click function so that an alert was displayed instead of the console log being used.

  • For me, the problem is usually creating the correct selector when I'm using jQuery.

    Hope that this helps you out.
     
    Sheriff
    Posts: 67636
    173
    Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    The correct selector for an anchor that has a class is a.classname

    The selector .classname a would select an anchor inside an element with the class name.
     
    Pete Letkeman
    Bartender
    Posts: 1868
    81
    Android IntelliJ IDE MySQL Database Chrome Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks for clearing this up Bear.

    Yep, one of the many things that I have yet to master selectors in jQuery.
    However,  I can usually find a solution to after some trial and error.
     
    If you two don't stop this rough-housing somebody is going to end up crying. Sit down and read this tiny ad:
    free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
    https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
    reply
      Bookmark Topic Watch Topic
    • New Topic