Today's Posts Follow Us On Twitter! TFL Members on Twitter  
Forum search: Advanced Search  
Navigation
Marketplace
  Members Login:
Lost password?
  Forum Statistics:
Forum Members: 24,256
Total Threads: 106,575
Total Posts: 592,698
There are 99 users currently browsing (tf).
 
  Our Partners:
 
  TalkFreelance     Design and Development     Programming     Javascript :

jQuery: Selecting clicked element?

Thread title: jQuery: Selecting clicked element?
Reply    
    Thread tools Search this thread Display Modes  
06-05-2012, 06:08 PM
#1
Dan is offline Dan
Dan's Avatar
Status: Request a custom title
Join date: Feb 2005
Location:
Expertise:
Software:
 
Posts: 3,164
iTrader: 15 / 86%
 

Dan is an unknown quantity at this point

  Old  jQuery: Selecting clicked element?

I have:

HTML Code:
<a href=# class=star>star</a> 
<a href=# class=star>star</a>
<a href=# class=star>star</a>
<a href=# class=star>star</a>
<a href=# class=star>star</a>
<a href=# class=star>star</a>
<a href=# class=star>star</a>
How can I target the element that is clicked?

I've tried:

Code:
$("a.star").click(function() {

var index = $(this).index();
index.addClass("fave");
But it doesn't work in this case. =(

06-05-2012, 06:18 PM
#2
Village Genius is offline Village Genius
Village Genius's Avatar
Status: Geek
Join date: Apr 2006
Location: Denver, CO
Expertise: Software
Software: Chrome, Notepad++
 
Posts: 6,892
iTrader: 18 / 100%
 

Village Genius will become famous soon enough

  Old

I don't see any issues in the code. The first thing you need to do is see what part is failing, put an alert inside that function so make sure it is going off when it should. That will tell you if it has to do with not being triggered or not being modified correctly.

06-05-2012, 06:57 PM
#3
Dan is offline Dan
Dan's Avatar
Status: Request a custom title
Join date: Feb 2005
Location:
Expertise:
Software:
 
Posts: 3,164
iTrader: 15 / 86%
 

Dan is an unknown quantity at this point

  Old

console.log is spitting "Object 7 has no method."

Meh. Guess I'll be re-reading documentation tonight.

Reply With Quote
06-26-2012, 01:48 AM
#4
Eirik-M is offline Eirik-M
Status: Member
Join date: Apr 2008
Location:
Expertise: Design
Software: Photoshop
 
Posts: 162
iTrader: 0 / 0%
 

Eirik-M is on a distinguished road

  Old

Originally Posted by Dan View Post
console.log is spitting "Object 7 has no method."

Meh. Guess I'll be re-reading documentation tonight.

I believe the problem is this. You declare index as

Code:
var index = $(this).index();
This is an integer, not an object. Therefore this fails (the integer has no method called "addClass"):

Code:
index.addClass("fave");
I believe this is what you want:

Code:
$(this).addClass("fave");

Reply    


Thread Tools
Display Modes

  Posting Rules  
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump:
 
  Contains New Posts Forum Contains New Posts   Contains No New Posts Forum Contains No New Posts   A Closed Forum Forum is Closed