1

So I Want my page to redirect to different page when a specific value of select is chosen. Whether it changes the value or not. If the value is already some other value the on Change works fine but if the value is already selected as the value which redirects the page it doesn't works for example

<script>
$("select").on('change', function() {
        if ($(this).val() == 'option2') {
            var data = $(this).attr("data_val");
            var sid = $(this).parent().parent().children()[3];
            var sid_value = $(sid).children()[0].value;
            window.location = 'new.php?data=' + data + '&sid=' +   sid_value;
        }});
</script>

if the select is set to option1 and i select option2 from the dropdown then the page redirects but when the option2 is already selected in the select and i open the dropdown again & click on option2 it doesn't redirect, please help me to achieve this.

Manthan Jamdagni
  • 894
  • 2
  • 17
  • 31
  • Possible duplicate of [Is there an onSelect event or equivalent for HTML – Jonas Flaam Jan 10 '17 at 10:54
  • Possible duplicate of [Trigger the event when selected the same value in dropdown?](http://stackoverflow.com/questions/20354112/trigger-the-event-when-selected-the-same-value-in-dropdown) – JJJ Jan 10 '17 at 12:22

2 Answers2

0

Well i found a Workaround for this problem i'll post it here if anyone faces the same problem. Here's the code

    <Script>
    var click1Done = false;
        $("select").on('click', function() {
            customSelect($(this));
            click1Done = true;
        });

        function customSelect(obj) {
            el = obj[0];
            if (click1Done && el.value === 'option2') {
                click1Done = false;
                //your code here to perform any extra task    
                window.location = 'new.php';
            }
        }</script>

Here i am seeing the no. of click on select if it is the second click on the desired option ('here option2') i redirect to the 2nd page else i don't.

Manthan Jamdagni
  • 894
  • 2
  • 17
  • 31
-2

consider this

$("select option").on('click', function () {
 // your code here
});
Romko
  • 1,808
  • 21
  • 27