I want to make function working only on page with specified element.
I have search page, search fields and results are in search-block div.
I want to bind function to this block, so function will not work on other pages (without <div id='search-block>...</div>)
I have next js code atm:
$(document).ready(function()
// instructions for another page (handlers for some links)
$(function(){
setInterval(findSomething,1000);
});
});
It working fine on page with search-block div, but it works fine on the other pages too. Browser tries to run this function on all other pages. I don't need this.
I tried jquery bind, but it now worked for me, don't know why :(
$("#search-block").bind(function(){
setInterval(findSomething,1000);
});
How I can bind handler to speciges too.fied block?
$0"$search-block")— illegal character there (0). I can't use$(document).ready(function(){ ... }in this script, because I already used it in another script.$().ready()(read this answer for additional information). Regarding to your question, I'd move all "search" logic tosearch-block.jsand insert<script src="search-block.js"></script>to the page with $("search-block") object on. BTW you may want to read about something like requirejs or about any modular design pattern.