html - Applying variable to element jquery -


i'm wondering if can in jquery. i'm using version 1.10:

var myimage = $('#myimage'); 

what want assign image variable can use anywhere. want use during event handling:

e.g.

myimage.click(function () {     alert('image clicked'); }); 

if above code incorrect, know appropriate code.

yes can it. in fact, recommended assign variable selector not evaluated each time use it. performance reasons.

finding element id (such in case $('#myimage');) fast imagine if have selector like

$('ul > li.myclass:has(p) a:eq(2)')

using variable speed things in such scenarios.

i use $ sign in variable name like. var $varname = $('.selector') helps me better understand code , distinguish between regular variables , variables contain dom elements.


Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -