How do you chain a jquery function to a vanilla selector

477 Views Asked by At

It's probably simple, but I can't figure it out or word it correctly for search engines. I'm trying not to use jquery where possible for a.performance and b.personal practice.

I am selecting an element

var container = document.getElementById('container');

Then I try to chain this jquery method on it

container.backstretch('url/to/img');

The jquery plugin I'm using is called backstretch.js

Is there an easy way to do this? Or does the answer rely on how the method was written?

Thanks!

Edit:

Console displays this error:

Uncaught TypeError: container.backstretch is not a function

Edit:

$(container).backstretch('url/to/img'); works. But I don't fully understand it. Is this because jQuery needs to create an object to attach the method to?

1

There are 1 best solutions below

0
PeterKA On

No sure if there's any performance savings, especially this being an ID selector, but, this is how:

var container = document.getElementById('container');
$(container).backstretch('url/to/img');

Or simply just use:

$('#container').backstretch('url/to/img');