0

I have a piece of javascript in a template which select an option from a select dropdown and causes a .change effect when loading a page.

This works fine. However instead of having this in the template file directly i want to add the funciton in a javascript file and call it in my functions when i am on a certain page.

I have trouble setting up the timeout element of my function for a .change to happen.

Below is my working script directly in the template file

<script>

$(document).ready(function(){
$("select").val('56');

window.setTimeout(function() { jQuery('.level-1').change();}, 0.5);

});
</script>

So now my rewritten function in a file.js

This is my functions file to call the js file

add_action( 'wp_footer', 'load_js');
function load_js() {

  wp_enqueue_script( 'jquery-ui-core3', get_stylesheet_directory_uri() . '/assets/jquery331.js');


  if (is_page ('395')) { 
 wp_enqueue_script( 'scriptfile1', get_stylesheet_directory_uri() . '/assets/page395.js');
  }

}

and this is my page395.js file

jQuery(document).ready(function($){
$("select").val('56');

    window.setTimeout(function() { 
       jQuery('.level-1').change();}, 0.5);


});

I dont know how to rewrite the windpws.setTimeout function. The one above is not working. Thanks!

1 Answer 1

2

You have not used settimeout function properly. Try below

jQuery(document).ready(function($){
    $("select").val('56');

    setTimeout(function() { 
       jQuery('.level-1').change();
    }, 1000); //here 1000 means 1 second

});
Sign up to request clarification or add additional context in comments.

1 Comment

@Missblues if you have tried this one and get helpful than make it as correct answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.