2

I am looking for a good way to handle a link click of different lines Each line is added at different times and i want only the most recently added line's links to be clickable. they are all under the same div tag

ex:
line 1:  "this is a line <link here>"  <-- dont want these to be clickable
line 2:  "this is a line <link here>"  <-- but still viewed
line 3:  "this is a line <link here>"  <-- most recent line clickable

hopefully this is being explained correctly the lines are added with .before function

So i added

to indicate each line. the link are indicated by a class id called talk_action and each line can contain more than one link. so somehow i need to set up an on click for the .talk_action which is clicked under the last paragraph. when the link is clicked it grabs the link id and passes it to a php file

ex:
<p>would you like to <a class='talk_action' id='shop'>shop</a> or talk about <a class='talk_action' id='quest'>quests</a></p>
user2376313
  • 59
  • 1
  • 10

4 Answers4

3

Assuming all the lines are in a div with id="lines"

$("#lines").on("click","a:last",function(){
     // your action here
});

This uses:

Here is a demo

Community
  • 1
  • 1
Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
  • @user2376313 Suggestion only.....if you are appending the links dynamically then you need to bind click event to a parent(existing) element. – Peeyush Oct 28 '13 at 08:40
  • No, still working on it. My links are dynamic and each line can contain more than one link. – user2376313 Oct 28 '13 at 09:23
2

The ':last' selector may suitable for you

$( "a:last" )
pszaba
  • 1,034
  • 9
  • 16
0

Hi the below code will work as expected.. http://jsbin.com/afUKiJi/1/edit

$(".clickable").find('a').last().on('click',function(){
alert('last');
})
codebreaker
  • 1,465
  • 1
  • 12
  • 18
0

Here is a jsFiddle. This will also take into account recently added anchors and is using event delegation in jquery.

$('div').on('click','a:last', function(){
console.log(this);
});
eggward
  • 345
  • 1
  • 5