How can I pass jQuery
selector from one function to another?
Does it work like passing variables from one function to another?
i.e:
function hasSelector(){
var items = $('#item1, #item2, #item3');
return items;
}
function useSelector(){
//var items = $('#item1, #item2, #item3');
//items.on('click'...);
hasSelector().on('click', function(){
alert(items);
)};
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Edit: Thanks to some clarifications in comments I've decided to change hasSelector()
into object
(I had to pass parameter aswell which i forgot to mention here. Nonetheless I've still used a function.)
var hasSelector = {
items : function(){
return $('#item1, #item2, #item3');
}
}
function useSelector(){
hasSelector.items().on('click', function(){
alert(hasSelector.items(this));
});
}
useSelector();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="item1">Item 1</div>
<div id="item2">Item 2</div>
<div id="item3">Item 3</div>
//returning [obj Obj] since I'm calling on all 3 items at once.