0

So, I have the following custom JavaScript in a page. (Written exactly the way it shows, except the img src)

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
  jQuery("#first").click(function(){
    jQuery("#second").trigger('click');
       return false;
  });
</script>

 <a id="first" href="#"> <img src="demo"> </a>

So the by clicking "first" button, it will trigger "Click" on the second button. The functionality itself works (ie. the "second" button is triggered)

However all other JavaScript functions are not working.

I am guessing adding a custom JavaScript file directly into the page is not the best way.

I see that there are some plugins to add JavaScript in a post/page, but I am trying to avoid any unnecessary plugins.

What would be the best way to add the custom JavaScript without affecting other JavaScript functions? What am I doing wrong?

Thanks

1

First of all, you should declare your scripts using wp_enque_script.

Second, jQuery is loaded automatically in WordPress and is set to no conflict mode. See the documentation for how to link a script that depends on jQuery.

Lastly, if you want it to appear on just one page you have two options

  1. In your functions.php file add a custom field to either enable this script on that page.

  2. Manually insert it on a given page, again via functions.php To target only the home page is_home() but of course you could just reference a page by it's page_id like this get_page( $page_id ).

  • I see. So the best way to do is by using the function.php instead of adding it in the page itself. That makes sense. Thanks Jean-pierre! – steve Kim Apr 22 '15 at 23:20
  • That is correct and you're welcome. – jeanpier_re Apr 22 '15 at 23:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.