Anonymous Vagrant
Anonymous Vagrant

Reputation: 121

If statement on a select box value in a javascript function

I am having problems writing a function in javascript on a PHP page that points to a select box. This if statement is to point to the select box

if (!(document.getElementById("add_product").value)==="Choose here")

Here's the code:

                     function promptEnrollmentMonth() {
                         if (!(document.getElementById("add_product").value)==="Choose here") {
                             var month = prompt("Is this the correct month for this course/product?", "<?php echo "$EnrollmentMonth"; ?>");
                             if (month !=null) {

                             }
                         }
                     }

 <button type="submit"
 onclick="promptEnrollmentMonth()">Update</button>

 <div>Add course/product:<select name='add_product'>
 <option selected>Choose here</option> 
 <option>Other options...</option>
 </select></div>

Upvotes: 1

Views: 69

Answers (2)

JoeCrash
JoeCrash

Reputation: 532

you have a typo in the if statement

if (!(document.getElementById("add_product").value)==="Choose here")

should be

if (document.getElementById("add_product").value !=="Choose here")

you need to set the value to choose here for the initial option. Or

if (document.getElementById("add_product").value !=="")

this case would apply if you don't want to set a value to the initial select option.

!(document.getElementById("add_product").value)

would just translate to true or false, which would never equal "Choose here"

Upvotes: 1

Binary Brackets
Binary Brackets

Reputation: 504

First issue is with "<?php echo "$EnrollmentMonth"; ?>"

it should be like :

var month = prompt("Is this the correct month for this course/product?", "<?php echo $EnrollmentMonth; ?>");

Second <option selected>Choose here</option> you haven't provide value attribute to option tag :

<option value='Choose here' selected>Choose here</option> 

Upvotes: 3

Related Questions