I created a JS and PHP code to query a database and display matching rows on a page. It was messing up other pages on WP, so I wanted to know how to target a single page so the JS doesn't run on other pages.


jQuery(document).ready( function() {
    console.log("Document loaded");
    jQuery("#searchButton").click( function(e) {
      console.log("Search button clicked");
      var zipCode = document.getElementById("inputField").value;
      var data = {
        'action' : "zip_code",
        'zip_code' : zipCode

      console.log("Zip code entered: " + data.zip_code);
      console.log("WP AJax URL: " + data.action);
         type : "post",
         dataType : "json",
         url : data.action,
         data : data,
         success: function(response) {
            if(response.success) {
                console.log("response.type == success");
            else {
                console.log("response.type == else");
         error: function(errorThrown){
            console.log("In error, error thrown!");



add_action( 'wp_enqueue_scripts', 'enqueue_parent_styles' );
function enqueue_parent_styles() {
   wp_enqueue_style( 'parent-style', get_template_directory_uri().'/style.css' );

add_action('wp_ajax_zip_search', 'zip_search');
add_action('wp_ajax_nopriv_zip_search', 'zip_search' );
function zip_search()
    global $wpdb;
    //  Functional code omitted for now.  Just need to get "here" paragraph return (for now)
    $output = "<p>here</p>";
    $response = array(
        'data' => $output,


add_action( 'wp_enqueue_scripts', 'my_load_scripts' );
function my_load_scripts() {

    // Enqueue javascript on the frontend.
        get_template_directory_uri() . '-child-agent_search/js/zip_search.js',

    // The wp_localize_script allows us to output the ajax_url path for our script to use.
        array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) )

  • 1
    Your code is triggered by clicking #searchButton. Just don’t put that button on other pages and it won’t run. Jul 23, 2020 at 13:46
  • 1
    @JacobPeattie try to post answers as answers, I can't give rep for a comment :( Remember answers can be improved in the future, and inspire others to write better answers
    – Tom J Nowell
    Jul 23, 2020 at 14:15
  • @JacobPeattie - Thank you! Jul 23, 2020 at 15:10

1 Answer 1


Here, the code operates on anything that matches the #searchButton element:


If there is a searchButton on other pages, it will match it, soo you need to be more specific.

For example, on the homepage, the <body> tag will have the home class, here is my sites homepage body tag:

<body class="home blog logged-in admin-bar no-customize-support">

So to make it target only the search button on the home page, you could try something like this:

jQuery(".home #searchButton")

Alternatively, only enqueue the JS on that particular page, but the selector method is superior. Make sure there is only one element on the page with that ID as IDs need to be unique

  • Just curious why the selector method is better. I tend to rely on enqueuing JS only where it's needed to keep the site as lightweight as possible.
    – WebElaine
    Jul 23, 2020 at 14:22
  • 1
    Because it lets you load the JS in advance, and saves additional requests. It's also simpler, and avoids a more complex error prone PHP based check. It also gives more flexibility. The OP could move their functionality into a block or shortcode, and allow the OP to place their functionality anywhere on their site in the future without being locked to a whitelist of URLs or post IDs
    – Tom J Nowell
    Jul 23, 2020 at 14:26
  • 1
    @TomJNowell - Thank you for that! Ok, let me formulate a new question to solve the next issue! Thanks again! Jul 23, 2020 at 15:09
  • @TomJNowell - Moved my code to a child theme (I created) and activated it. It worked! Turns out my biggest issue all along was having the a duplicate element ID. Thanks again! Most recent question for reference: wordpress.stackexchange.com/q/371655/184205 Jul 23, 2020 at 16:29

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.