Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

174
Views
Disable the button until the user selects a valid option

I have a small search engine consisting of 2 select "category" and "province". I would like that until the user selects a valid category or location, the button is disabled. For UX reasons I entered the deselected text "select category" and "select location". The problem is that if the user doesn't select anything, and leaves one or both values ​​(select category, select location) and presses the button, the search engine returns me an error page! So I thought about the solution of blocking the button until the user makes a valdia choice (which excludes the text in option)

This is the page code:

                  <div class="listing-search-field category">
                        <select name="cat" id="category" >
                        <option selected disabled>Select Category</option>; 
                <?php if(is_array($categories) && count($categories)){
                    foreach($categories as $key => $value){?> 
                        <option value = "<?php echo esc_attr($value)?>">
                            <?php echo esc_attr($value)?>
                        
                        </option>
                    <?php }
                } ?>
            </select>
        </div>
        <div class="listing-search-field location">
            <select name="location" id="location">
            <option selected disabled>Select Location</option>;
                <?php
                    if(is_array($locations) && count($locations)){
                        foreach($locations as $key => $value){ ?>
                            <option value = "<?php echo esc_attr($value)?>">
                                <?php echo esc_attr($value);?>
                            </option>
                        <?php }
                    } ?>
            </select>
        </div>
    </div>
    <div class="listing-search-submit-holder">
        <?php
        if ( listing_theme_installed() ) {
            echo my_theme_get_button_html(array(
                'type' => 'solid',
                'text' => esc_html__('Search Places', 'eltd-listing'),
                'html_type' => 'button',
                'hover_border_color'   => '#fff',
                'hover_color'   => '#fff',
                'icon_pack' => 'font_elegant',
                'fe_icon' => 'arrow_carrot-right',
                'size' => 'large'
            ));
        }
        ?>

I'm not a programmer, so a concrete example based on this code would be very welcome.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Hello Andrea and welcome to stackoverflow. What you need can be easily done using javascript. Let me walk you through the process.

  1. You need to identify what elements are important for you.

From your example it seems you have 3 important elements you want to watch or change. 2 select elements and submit button.

You can select elements like this:

<script>
let firstSelect = document.querySelector('#category');
</script>
  1. You need to determine what actions you want to watch for.

In your example you want to watch those 2 select elements if any of them has option selected.

This can be done like this:

firstSelect.addEventListener("change", myLittleFunction);

This piece of code is watching the element we named firstSelect and whenever it changes (user selects a different option) it runs the function called myLittleFunction (we create this function in the next step).

  1. Decide what conditions needs to be met and what should happen

In your example you have very simple condition - user needs to select any option any one of those 2 select elements. And once this condition is met you want to do something with the button.

In order to achieve the above goal we can do this:

function myLittleFunction(){
  if (firstSelect.options[firstSelect.selectedIndex].value){
    //Change the button in the way you need (enable)
} else {
    //Change the button in the way you need (disable)}
}

This function checks if the element we named firstSelect has any value (if the selected option has any value), if so it can run whatever you want to do. In case the selected option has no value, it can do something else. What exactly should happen is up on you (you can display/hide the button, you can add/remove class, etc.)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!