0

How do you reference a button by 'this' and pass it into a function?

$(document).on("click", ".join-group-btn", function () {
                    var groupid=$(this).val();
                    var button=$(this);
                    joinGroup(groupid, button);
                });

Then I keep the joinGroup function in my library on a separate page:

function joinGroup (groupid, buttonIn) {
    var button = $(buttonIn);
    var l = Ladda.create(document.querySelector(buttonIn));
}

Id's won't work as the buttons are generated dynamically.

This throws:

Dom exception 12: An invalid or illegal string was specified

Thanks for the help! It is greatly appreciated.

ambe5960
  • 1,870
  • 2
  • 19
  • 47
  • What does `Ladda.create` expect as an argument, and what does it do? – Musa Mar 30 '15 at 21:11
  • You already sending button as an element. You don't have to do `$(buttonIn)` , `buttonIn` is already the element u want. – Batu.Khan Mar 30 '15 at 21:11

2 Answers2

0

You should use querySelector with CSS-alike syntax selectors

your buttonIn is a jQuery object element

document.querySelector(buttonIn) // you need to pass a String selector
                                 // like "#someId",
                                 // not an jQuery object

An example would be:

$(document).on("click", ".join-group-btn", function () {
    joinGroup( this.value,  $(this),  this.id  );
});


function joinGroup ( value,  $this,  id) {
    // console.log( value );     // "Some Value"
    // console.log( $this );     // [Object] the clicked jQuery Element 
    // console.log(  id   );     // "someID"
    // Now if you want to use querySelector
    var theButton = document.querySelector("#"+ id); // The missing #
    // Or Directly operate over the `button` Element object argument
    // $this.css({color:"red"}); // Cause it's a jQuery element
}
Roko C. Buljan
  • 196,159
  • 39
  • 305
  • 313
0

document.querySelector expect a selector string. But your function needs a DOM-object right? You can pass it so:

function joinGroup (groupid, buttonIn) {
    var l = Ladda.create(buttonIn[0]);
}
RWAM
  • 6,760
  • 3
  • 32
  • 45