Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery event on attribute change

Tags:

html

jquery

I need a function to run when a divs data-page-index attribute changes

  var active = $('.swipeview-active'),
      dpi =  parseInt(active.attr('data-page-index')),
      left = $('[data-page-index="' +prev+ '"]').children("img").eq(0),
      right = $('[data-page-index="' +next+ '"]').children("img").eq(0);

    $(active.attr('data-page-index')).change(function(){

    right.clone( true ). css({'z-index': 5}). fadeIn(2000).appendTo('#right');
    left.clone( true ). css({'z-index': 5}). fadeIn(2000).appendTo('#left');
    });

I tried the change() function but that only seems to work with input fields,

    $(active.attr('data-page-index')).change(function(){    

is there another way of achieving this? Thanks.

like image 387
Tim Wilkinson Avatar asked Jan 10 '13 16:01

Tim Wilkinson


1 Answers

It seems that you still have to manipulate the DOM to trigger the event? If so, you can manipulate a hidden input's value instead of manipulating other elements' data attributes. Then you can use the change trigger.

  var active = $('.swipeview-active'),
      dpi =  parseInt(active.attr('data-page-index')),
      left = $('[data-page-index="' +prev+ '"]').children("img").eq(0),
      right = $('[data-page-index="' +next+ '"]').children("img").eq(0),
      dpiInput = $('.swipeview-active input:hide');

  dpiInput.change(function(){
    right.clone( true ). css({'z-index': 5}). fadeIn(2000).appendTo('#right');
    left.clone( true ). css({'z-index': 5}). fadeIn(2000).appendTo('#left');
  });

trigger:

$('.swipeview-active input:hide').val(1).trigger('change');
like image 105
Gabriel Cheung Avatar answered Oct 16 '22 15:10

Gabriel Cheung