-4

I'm trying to append to a non DOM element (Pagiantion added for a slideshow).

My code is:

$("#slides2 .slidesjs-pagination-item a").on("click", function (event) {
    alert('append');
});

which does not give me an alert. As on is replacing live, I'm wondering why this doesn't work?

UPDATE The comment by adeno helped, and my code is now :

$(document).on("ready", "#slides2 .slidesjs-pagination-item a", function (event) {
    $("#slides2 .slidesjs-pagination-item a").prepend("Image");
    $("#slides2 .slidesjs-pagination-item a").append(" of 30");
});

which doesn't work, how do I attach an event that recgonises when it is the element is loaded?

Community
  • 1
  • 1
  • `$(document).on("click", "#slides2 .slidesjs-pagination-item a", function(event){...` – adeneo Nov 29 '13 at 22:41
  • possible duplicate of [Event binding on dynamically created elements?](http://stackoverflow.com/questions/203198/event-binding-on-dynamically-created-elements) – charlietfl Nov 29 '13 at 22:42
  • 2
    I don't see what your question has to do with appending, nor why you're calling this a "non DOM element". – Blue Skies Nov 29 '13 at 22:47
  • Im just using the click function and alert to test .on, by non DOM element i mean it doesnt exist unti the document is ready – TwistedPixel88 Nov 29 '13 at 23:00
  • `$(document).on("click", "#slides2 .slidesjs-pagination-item a"`, function(event){ } worked thanks, so now my code is `$(document).on("ready", "#slides2 .slidesjs-pagination-item a", function(event){ $("#slides2 .slidesjs-pagination-item a").prepend("Image "); $("#slides2 .slidesjs-pagination-item a").append(" of 30"); });` Which doesnt work, how do i attach an event that recgonises when it is the element is loaded? – TwistedPixel88 Nov 29 '13 at 23:17
  • @user3050844 your question is very poor. create a jsfiddle – thenewseattle Nov 29 '13 at 23:38

4 Answers4

1

You must bind the event to the parent element, then select the target element using the selector argument. You can find more information about .on() here.

$('#slides2').on('click', '.slidesjs-pagination-item a', function(e) {
    alert('append');
});
Austin Brunkhorst
  • 20,704
  • 6
  • 47
  • 61
0

Try:

$("#slides2").on("click", ".slidesjs-pagination-item a", function(event){
    alert('append');
});

Though it's hard to tell without seeing any HTML...

benhowdle89
  • 36,900
  • 69
  • 202
  • 331
0

Two potential pointers:

  1. Does the line below return a number > 0? If not your selector is wrong.

    alert($("#slides2 .slidesjs-pagination-item a").length)

  2. You mention that you trying to append to a non DOM element. Is the element you select attached to the DOM at all? If not: you cannot click on a non DOM node.

Koen Peters
  • 12,798
  • 6
  • 36
  • 59
0

$("#blah") wont work if the element is not in the DOM. I guess your template is in parameter, so do this:

$("#blah", myTemplate).click(...
Noampz
  • 1,185
  • 3
  • 11
  • 19