0

I have html like so

<span rel='comm' val='12'>click</span>
<span rel='comm' val='82'>click</span>

and I am using JQuery to do this

$('span[rel*=comm]').cust();

and the custom function is as such

$.fn.cust = function () {

    $(this).click(function(e) {
        alert($(this).val());
    });
}

The value of this is 12 even when I click on 2nd span which should give me 82

Any help would be appreciated.

1

6 Answers 6

2

You'll need to return a seperate function for each element in the collection, normally done with return this.each ...

$.fn.cust = function () {
   return this.each(function() {
      $(this).click(function(e){
          alert($(this).val());
       });
   });
}

And value is not a valid attribute for a span element.

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

3 Comments

@user1690718 - see the link by Matt Ball in the comments. And your HTML is'nt valid !
yes i know :) just needed to give an example , im recovering attr value chhers
@user1690718 - If you intend to use attr('value'), that's a hack for something that is inherently wrong and invalid.
0

This should work better:

$.fn.cust = function () {
    $(this).click(function (e) {
        alert($(this).attr('val'));
    });
}

span does not have value.

http://jsfiddle.net/dREj6/

Also if you want to make your method chainable you should return an jQuery instance:

$.fn.cust = function () {
    return $(this).click(function (e) {
        alert($(this).attr('val'));
    });
}

$('span[rel*=comm]').cust().css('color', 'red');

http://jsfiddle.net/dREj6/1/

Comments

0
  • rel are for links (anchor element) - use class
  • use data attribute instead of custom attributes

http://jsbin.com/ogenev/1/edit

<span class='comm' data-val='12'>click</span>
<span class='comm' data-val='82'>click</span>

$.fn.cust = function(){

   $(this).click(function(){
      alert(this.dataset.val);
   });

};


$('.comm').cust();

Comments

0

It works if you use .attr('val')

$.fn.cust = function () {

    $(this).click(function(e){
      alert($(this).attr('val'));
    });
}
$('span[rel*=comm]').cust();

http://jsfiddle.net/fW7FT/

.val() is for input since they're the only one accepting the val attribute officialy

Comments

0

The call $('span[rel*=comm]') returns a JQuery wrapper for all spans matching the selector - the two ones you have in your example are picked both.

Now inside the definition of cust, $(this) refers to the wrapped array, which causes your issue. Use

$(this).each( function() { 
     $(this).click (...
});

Inisde each $(this) will point to each separate span element in the selection, so they will have the click handler individually attached and working as you expect.

Comments

0

You can achieve what you're looking for with this:

HTML:

<span rel='comm' val='12'>click</span>
<span rel='comm' val='82'>click</span>

JS:

var cust = function(source) {
  alert($(source).attr('val'));
}

$('span[rel*=comm]').click(function(e) {
 cust(this);
});

The JSFiddle working: http://jsfiddle.net/ejquB/

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.