Prashank
Prashank

Reputation: 796

Show a popup when a dropdown value is selected with only js

I want a simple js script to show a popup when a specific option from the dropdown is selected. The name of the selection tag is "ProfileType[0]"

It does not have a id attribute and it contains those special character. I tried to use jquery but no luck with it. And i would prefer if the selection tag code will be untouched means without adding any onchange event to it. And one more thing it already have a onchange event. I know its possible with jquery. You have any good code for this.

<select name="ProfileType[0]" onchange="window.location.href = "join.php?pid=" + this.value;">
<option value="1">Personal</option>
<option value="2">Professor</option>
<option value="4">Student</option>
</select>

Any help will be very much appreciate. Thanks

Upvotes: 0

Views: 4581

Answers (2)

nnnnnn
nnnnnn

Reputation: 150040

Using jQuery:

$(document).ready(function() {
    $('select[name="ProfileType\\[0\\]"]').change(function() {
        if ($(this).val() === "1") {
            alert("Your popup here");
        }
    });
});

Demo: http://jsfiddle.net/YVNMK/

You don't say which option is the specific one you care about, so I've gone with the first one.

Note that attaching the handler with jQuery should not affect the inline onchange, though your existing onchange is invalid because you've tried to use double-quotes inside other double-quotes - change one or other set of doubles to single-quotes:

onchange="window.location.href='join.php?pid=' + this.value;"

Upvotes: 1

Simon Edstr&#246;m
Simon Edstr&#246;m

Reputation: 6619

$("select[name='ProfileType[0]']").change(function (){
   if($(this).val() == 1){ // Check if the selected value is 1
    // Create the popup
   };
});

I hope this point you in the right direction

Upvotes: 1

Related Questions