1

I would like a function to run when a specific anchor with the `value="frb" is clicked.

This is the anchor

<a href="<?php echo $l1; ?>" value="frb" class="button">Accept</a>

this is what i tried:

$('body').on('click', value[frb], function(e) {
    e.preventDefault();
});

this doesnt work, i cant find any examples that use this, is it possible?

6
  • 1
    i don't think you should use value attribute in a link element. Maybe you should use class="frb" Commented Jun 24, 2012 at 0:51
  • @DavidCheung is there a specific reason i shouldn't? The class it already uses is being used to css reasons. Commented Jun 24, 2012 at 0:52
  • 1
    I don't think that 'value' is a valid attribute on an 'a' tag. I haven't seen it used before. Commented Jun 24, 2012 at 0:56
  • you can add the class as a span. i.e. <span class="frb"><a href....></a></span> it wouldn't make your jquery anymore complicated Commented Jun 24, 2012 at 0:59
  • 1
    An a element has no value attribute, as already mentioned, but rather than repurposing the class attribute, why not simply use a custom data-* attribute? Commented Jun 24, 2012 at 1:22

5 Answers 5

1

value is not valid attribute to anchor tag. Instead of that you can use data-value like following:

HTML

<a href="<?php echo $l1; ?>" data-value="frb" class="button">Accept</a>

jQuery

$('body').on('click', 'a[data-value=frb]', function(e) {
    e.preventDefault(); // prevent page reload
    alert( this.href );
});

Working sample

Note:

Already @Musa mentioned about data attribute, but in his jQuery snippet used a[value=frb] which will not work.

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

3 Comments

What is the difference between $('body').on('click', 'a[data-value=frb]', fn); and $('a[data-value=frb]').on('click', fn);?
@st-boost first one is for delegate event bing (aka live event) and second one is ordinary binding when element already exists in dom at page load
ok, thanks. Wouldn't the first method be considerably less efficient? Or does it only apply to nodes added by jQuery?
1

The second parameter of .on() should be a selector string

$('body').on('click', 'a[value=frb]', function(e) {
    e.preventDefault();
});

also a tag doesn't have a value attribute, you should use a data attribute instead

<a href="<?php echo $l1; ?>" data-value="frb" class="button">Accept</a>

Comments

0

try this:

$(function(){
    $('a[value=frb]').click(function(e) {
        e.preventDefault();
    });
});

It assigns the function to all anchors with the specified 'value' attribute.

Comments

0

working demo http://jsfiddle.net/KQy36/

OR http://jsfiddle.net/WP7JS/

API: http://api.jquery.com/on/

Oh by the way: http://www.w3schools.com/tags/tag_a.asp (To see valid a tag attribute)

Anyhoo, Hope this helps,

code

$('a[value="frb"]').on('click', function(e) {
    alert('yeah wha?');
    e.preventDefault();
});​

OR apart of another post below mine. :)

$('a').on('click', function(e) {

    if ($(this).attr("value") === "frb") {
        alert('yeah wha? I am frb');
        e.preventDefault();
    } else {

        alert('not frb');
        e.preventDefault();
    }

});​

Comments

0

Since there is no value attribute for the link tag we need to make a change to the html:

<span class="frb"><a href="<?php echo $l1; ?>" class="button">Accept</a></span>

Then add this to register the click event.

$(document).ready(function(){
  $('span.frb').click(function(e) {
    e.preventDefault();
  });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.