0

I'm trying to set focus to textbox in a modal using the code below:

$('#search-modal').on('shown.bs.modal', function () {
    $('#s').focus();
});

this code is in script.js file which I add it using the code:

function dia_scripts() {
   wp_enqueue_style( 'bootstrap', get_template_directory_uri() . '/css/bootstrap.min.css', array(), '3.3.7' );
   // checking to include Bootstrap rtl edit
   if ( is_rtl() ) {
        wp_enqueue_style( 'bootstrap-rtl', get_template_directory_uri() . '/css/bootstrap-rtl.min.css', array(), '3.3.7' );
    }
    wp_enqueue_style( 'style', get_template_directory_uri() . '/style.css' );
    wp_enqueue_script( 'bootstrap', get_template_directory_uri() . '/js/bootstrap.min.js', array( 'jquery' ), '3.3.7', true );
    wp_enqueue_script( 'custom-script', get_template_directory_uri() . '/js/script.js', array( 'jquery' ), null, true );
}
add_action( 'wp_enqueue_scripts', 'dia_scripts' );

I found that jQuery -blank javascript is working- isn't working with script.js file. However, it's working well with bootstrap.min.js file. any ideas?

4
  • WordPress jQuery object reference is jQuery, not $, check your browser's error console for javascript errors.
    – Milo
    Aug 29, 2017 at 17:00
  • It seems that you might use jQuery prefix for your js functions instead of $ Aug 29, 2017 at 17:00
  • Yep you are right. Thanks a lot Milo and @anton-lukin. but why bootstrap JavaScript is still working I don't think it uses jQuery prefix, right? Aug 29, 2017 at 17:11
  • look at the bootstrap.js file and you will see code wrapped inside function($){ //code here }(jQuery);. This passes $ as the object reference inside that function so you can use $.
    – Milo
    Aug 29, 2017 at 17:19

0

Browse other questions tagged or ask your own question.