0

I'm trying to detect click on a pagination anchor through a :data selector with no result:

<!DOCTYPE html>

<html>
<head>
    <script type='text/javascript' src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
</head>
<body>
    <ul>
        <li>
            <a href="#" data-pageindex="1">1</a>
        </li>
        <li>
            <a href="#" data-pageindex="2">2</a>
        </li>
        <li>
            <a href="#" data-pageindex="3">3</a>
        </li>
    </ul>


    <script type="text/javascript">
        $("a:data(pageindex)").click(function (event) {
            event.preventDefault();
            alert('anchor clicked!');
        })
    </script>
</body>
</html>

Any clues about what I am doing wrong?

2
  • :data() selector exists in jQueryUI but the thing is that it's not applicable in your case, check the docs: api.jqueryui.com/data-selector , you may opt to use Rajaprabhu Aravindasamy's answer. Commented Jun 21, 2014 at 15:55
  • If you still want to use your own logic (by using the :data selector), I think you have to customize your own :data selector (don't need jQuery UI), check this james.padolsey.com/javascript/… , with the .expr[':'], you can extend many selectors, not just :data. Commented Jun 21, 2014 at 15:57

3 Answers 3

3

Try to use Has attribute selector at this context,

$("a[data-pageindex]").click(function (event) {
     event.preventDefault();
     alert('anchor clicked!');
});

DEMO

Sign up to request clarification or add additional context in comments.

Comments

2

There is no ":data" selector in jQuery.

http://api.jquery.com/category/selectors/

Comments

0

This is not a valid selector. Instead use like this,

$("ul > li").each(function(){
     $(this).find("a").data();
     $(this).click(function(){
          console.log($(this).find("a").data().pageindex);
          // do your stuff
     })
});

Here's the Demo Fiddle Link

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.