0

I'm building new web page and it's supporting ios web application. When i try to open web app, my .delegate method is not firing. It's acting like i'm clicking <a href='#'></a> . Going to top of the page and method is not firing.

What i've done so far is

$(document).delegate("a[data-what='voteThis']", "click", function (e) {
            e.preventDefault(); 
                 .
                 . 
                 .
                });

And my html code is

<a href='#' data-what='voteThis' id='vote" + id + "'> vote </a>

i've tested on Android and there is no problem with it. Also i've tested Chrome and Safari browsers on Ios, and there is no problem with them. Only on ios web app makes problem.

1 Answers1

1

Unless you're using jQuery < 1.8 (which ... you shouldn't be), you should be using .on():

$(document).on('click','a[data-what="voteThis"]',function(e){
    e.preventDefault();
    ...
});

Also, please do not delegate the click event from the document ... it's horribly inefficient, you should delegate from as close of a parent as possible. Example:

$('.voteTheseContainer').on('click','a[data-what="voteThis"]',function(e){
    e.preventDefault();
    ...
});

And unless you have multiple types of elements that have the data element what, you can remore the a qualifier because it does the same thing but is faster:

$('.voteTheseContainer').on('click','[data-what="voteThis"]',function(e){
    e.preventDefault();
    ...
});

Hopefully this helps.

PlantTheIdea
  • 16,061
  • 5
  • 35
  • 40
  • i'm using jquery-1.9.1 and if i use .on('click'... function is firing but still goes to top of the page. and thank you for performance suggestion – user2956229 Mar 15 '14 at 19:05
  • ah, i see. in your HTML, instead of using `a href="#"`, use `a href="javascript:void(0)"`. you also won't need to use `e.preventDefault()` using that technique. – PlantTheIdea Mar 15 '14 at 19:17
  • yes using `a href="javascript:void(0)"` is solved my problem. but im not sure of that using javascript:void(0) is right. but that solved. thank you so much – user2956229 Mar 15 '14 at 19:49
  • @user2956229 - it's actually more correct than `href="#"`, much more effective, more transparent to SEO crawlers identifying link purpose, and is assuredly cross-browser compatible. glad it worked out for you though! – PlantTheIdea Mar 15 '14 at 19:52
  • now i have to check my all `href="#"` thank you for correcting me :) – user2956229 Mar 15 '14 at 19:58