2

How to convey a reference to "this" in a function?

 3 years ago
source link: https://www.codesd.com/item/how-to-convey-a-reference-to-this-in-a-function.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

How to convey a reference to "this" in a function?

advertisements

This question already has an answer here:

  • How to access the correct `this` inside a callback? 4 answers

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.


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]);
}

Related Articles

Pass by reference Using * this const member function does not compile?

I'm following along some sample code, but I'm getting an error from it that I don't know how to fix properly. I'm getting an error passing *this as a reference. Here's the relevant code: //ShadeRec.h class World; class ShadeRec { public: World& w; Sh

How do I pass parameters to this Jinja template function?

This works, but Id like to pass the parameter variable values to the section inside return render_template() so I can reuse test(), how can I do that? """ Test.html """ @app.route('/test') def test(): """Render

How to convey members of a dict to a function

Suppose I have a function that I do not control that looks something like the following: def some_func(foo, bar, bas, baz): do_something() return some_val Now I want to call this function passing elements from a dict that contains keys that are ident

How do I call a block defined by a parent with a new reference to this in Javascript

If I have a parent object in JS that has a block saved to a key, how can I call that in a nested object with the correct reference to 'this'. var colors = { foo: function () { return this.color; }, red: { color: 'red', myColor: function () { return f

How can I correctly reference this configuration file to not encode the path?

Im trying to reference this config file that is in the same folder as the class that contains this code. I'd like to do some type of relative reference to it, so I can use it from other places. When I try using just the file name without the path, th

XMLHttpRequest inside an object: how to keep the reference to & ldquo; This & rdquo;

I make some Ajax calls from inside a javascript object.: myObject.prototye = { ajax: function() { this.foo = 1; var req = new XMLHttpRequest(); req.open('GET', url, true); req.onreadystatechange = function (aEvt) { if (req.readyState == 4) { if(req.s

How to convey data from one view to another with custom events?

Say I have a View that displays a search box with a submit button. When I click on the submit button how do i pass the value of the search box to another view ? I tried: In view 1, inside the submit callback : this.trigger('abc', $('#searchBox').val(

How to add a reference by program

I've written a program that runs and messages Skype with information when if finishes. I need to add a reference for Skype4COM.dll in order to send a message through Skype. We have a dozen or so computers on a network and a shared file server (among

How do I get references to remote objects already instantiated using WCF?

I've just read Ingo Rammer's article titled "From .NET Remoting to the Windows Communication Foundation (WCF)", on the MSDN website (http://msdn.microsoft.com/en-us/library/aa730857(v=vs.80).aspx). I still have one doubt, though, and I hope some

How to initialize a reference to an object if the reference is a member of the class?

Let's say Class contains a reference called matrix_: Class.h class Class { Matrix& matrix_; } Class.cpp Class::Class() : matrix_(Matrix()) { } I get the error: invalid initialization of non-const reference of type 'Matrix&' from a temporary of typ

How to avoid circular reference OOAD design

I have Class structure like below, this below scenario will create a cyclic reference. How to avoid this situation? public class classA { public classA() { classB b= new classB(); b.classBMethod(); } public void classAMethod() { Console.WriteLine("Cl

How to get a reference in a structure of the same structure in my code

I have this struct, but I get some errors when I mention a reference to its own type: struct Point { int x; int y; bool isLattice; Vect2D gradient; ///used only when isLattice is false Point p00; ///top-left corner Point p01; ///top-right corner Poin

How do I serialize / reference App Engine users?

This is a follow-up to this question: How do I correctly reference Users in GQL queries on App Engine? In my app, I have a Photos db.Model, where each photo has a UserProperty. I want to display a list of users, where clicking on a user will show all

How to add a reference to an external DLL from the script task?

How can I add reference to an external assembly from within a script task and deploy the package to a server along with the newly added external assembly?If this is in reference to your other recent question, then the only proper way to get the corre

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK