Harry
Harry

Reputation: 23

call JS function from select box

I am using MVC pattern for building a PHP movie web. in View class, I print out the select box that contain an array.

print "
        <div>
            <div class='top-nav'>
                <label for='actor'>Actor:</label>
                <select name='actor' id='id-actor' onchange='movieFilterChanged();'>
                    <option value='all'> Select All </option>
    ";            
                    foreach($actors as $actor){
                        print "
                            <option value='".$actor['actor_id']."'> ".$actor['actor_name']." </option>
                        ";
                    }                          
    print "                
                </select>
            </div>
    ";

The JS function movieFilterChanged() will be triggered when value was selected. I wrote the simple JS function :

function movieFilterChanged(){
    var actor = document.getElementById('id_actor').value;
    if (actor != 'all')
        alert("actor");
}

But this JS function is not triggered.

Upvotes: 0

Views: 69

Answers (1)

Armando Mariscal
Armando Mariscal

Reputation: 117

Firts change the id from your select:

id='id_actor'

Try use alert without quotation marks:

alert(actor);

And you can add a else in your if, like this:

else {
 alert(actor);
}

OR use:

console.log(actor);

You will see the value of the actor and you will notice that you are using a different id

Upvotes: 1

Related Questions