BorisMoore / jsviews

Interactive data-driven views, MVVM and MVP, built on top of JsRender templates
http://www.jsviews.com/#jsviews
MIT License
856 stars 130 forks source link

Get jquery element for {on... #436

Closed alnico2001 closed 4 years ago

alnico2001 commented 4 years ago

Is there any method for getting the jquery element when using data-link="{on ~myFunction}"?

myFunction(msg, ev, eventArgs) {
     jqElement = $(this); //this of course does not work
     jqElement = ev.target; //not sure if this can be used to get it

     jqElement.parent().find('.something');
}

If I put an id on the element, I could pass that to handler, that would work I guess.

I couldn't find anything here https://www.jsviews.com/#link-events

BorisMoore commented 4 years ago

ev.target, and also eventArgs.linkCtx.elem will each return the HTML element.

If you want a jQuery object, you can do $(ev.target).

See this paragraph where it says:

Does that cover what you need?

alnico2001 commented 4 years ago

I did read that paragraph ;-)

My original (and only) test was $(ev.target).css('color', 'red'); // this does not work

Now after your response above, I did another test: $(ev.target).remove(); // this works

It appears that there are limitations?

However, when testing with this $(eventArgs.linkCtx.elem).css('color', 'red'); // this works

Note: I don't think I would have put this together (eventArgs.linkCtx.elem). I think I need to start a cheat sheet for myself ;-)

BorisMoore commented 4 years ago

$(ev.target).css('color', 'red'); works for me.

Also, I would expect ev.target === eventArgs.linkCtx.elem.

Can you provide a sample/jsfiddle where $(ev.target).css('color', 'red'); does not work?

BorisMoore commented 4 years ago

Closing for now, since not a JsViews issue, I think. But please provide a jsfiddle, if $(ev.target).css('color', 'red'); is not working, and I will reopen.

alnico2001 commented 4 years ago

Sorry for late response. Yes it works...I believe I had the function signature parameters/arguments mis-aligned. Apologies