Js in the browser console works, but not from the file.

jQuery(document).ready(function(){ $('.sf-with-ul').click(function(){ console.log('clicked'); }); }); 

Closed due to the fact that the essence of the question is not clear to the participants of zb ' , PashaPash , Alex , RussCoder , Max Mikheyenko 24 Oct '15 at 17:14 .

Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • four
    Check whether the file is connected at all, for example, before document.ready add alert () - dekameron
  • In the firewall, see if the file is connected to the page - Insight
  • Yes, everything is well connected. Alert works both before the code and after, but if you put it inside, it does not work. - DedGrisha

2 answers 2

Possible reasons for the error:

  1. JQuery library is not loaded yet.
  2. The element during the ready event does not yet exist.

Try this:

  console.log($); // Проверка, загружена ли вообще jQuery $(document).on('click', '.sf-with-ul', function() { console.log('clicked'); }); 

    Perhaps this element appears on the page after loading via AJAX . Try this:

     $(function(){ // тот же $(document).ready(), но короче $(document).on('click', '.sf-with-ul', function(event){ // вешаем событие на документ, что бы все добавленные элементы в процессе работы то же обрабатывались console.log('clicked'); }); }); 

    There are no errors in the console? Maybe you connect jQuery later your script?