1
<form class="uk-form-stacked">
    <label class="uk-form-label" for="kUI_dropdown_basic_select">Select a user</label>
    <select id="kUI_dropdown_basic_select" class="uk-form-width-medium" onchange="getIPL();">
        <?php  foreach ($response->result->allAcc as $data) { ?>
            <option  value="<?php echo $data->Id;?>">   <?php echo $data->Name; ?></option>
        <?php } ?>
    </select>
</form>


<script>
function getIPL() {
    var urlmenu = document.getElementById( 'kUI_dropdown_basic_select' );
    urlmenu.onchange = function() {
    window.open( 'admin_mailbox.php?Id=' + this.options[ this.selectedIndex ].value,"_self");
    };
}
</script>

Thank you for taking the time for reading my question. onchange() is not working for the first time second time it works fine

1
  • 1
    It likely is working. The change event just uses multiple handlers at different times. – The first occurrence of change invokes getIPL(), which just replaces itself as the handler with a different function. That 2nd function then opens new windows on each subsequent occurrence of the event. Commented Feb 25, 2016 at 5:58

4 Answers 4

3

Remove onchange from select box. And change your JS with this :

<script>

    var urlmenu = document.getElementById( 'kUI_dropdown_basic_select' );
    urlmenu.onchange = function() {
    window.open( 'admin_mailbox.php?Id=' + this.options[ this.selectedIndex ].value,"_self");
    };

</script>
Sign up to request clarification or add additional context in comments.

4 Comments

There is no onclick event
Oh yes it's onchage. Thanks.
also suggest to add a default <option value="">Select</option> otherwise ist option selected as default and will not work... maybe..
@devpro It works if user will change the value of select box.
1

When you are using document.getElementById for getting element id, than no need to use onchange() event in <select>

And i also suggest you to add one more option for selection <option>Select</option>

Test Example (change as per your PHP Code):

var urlmenu = document.getElementById( 'kUI_dropdown_basic_select' );
urlmenu.onchange = function() {
window.open( 'admin_mailbox.php?Id=' + this.options[ this.selectedIndex ].value,"_self");    	
};
<form class="uk-form-stacked">
    <label class="uk-form-label" for="kUI_dropdown_basic_select">Select a user</label>
    <select id="kUI_dropdown_basic_select" class="uk-form-width-medium">
        <option value="">Select</option>
        <option value="test1">Test1</option>
        <option value="test2">Test2</option>
    </select>
</form>

1 Comment

Thanks for the help :)
1

Very easy and simple code below

<form class="uk-form-stacked">
   <label class="uk-form-label" for="kUI_dropdown_basic_select">Select a user</label>

   <select id="kUI_dropdown_basic_select" onmousedown="this.value='';" class="uk-form-width-medium" onchange="getIPL(this.value);">
      <option  value="aaa">aaa</option>
      <option  value="bbb">bbb</option>
      <option  value="ccc">ccc</option>
      <option  value="ddd">ddd</option>
   </select>
</form>

javascript function

function getIPL(value) {
  //var urlmenu = document.getElementById( 'kUI_dropdown_basic_select' );
  //urlmenu.onchange = function() {
  window.open( 'admin_mailbox.php?Id=' + value,"_self"); 
  //};
}

6 Comments

this will work alert(value); but not this urlmenu.onchange = function() {
you don't need to use utlmenu.onchange event again...b'coz it's already working for onchange event
just use : window.open( 'admin_mailbox.php?Id=' + value,"_self");
it's so easy, also you can use jquery for same thing
i have added it in answer
|
-1

You have already defined onchange event of select box here...then why you use onchange again on same element.

Use following code

<form class="uk-form-stacked">
    <label class="uk-form-label" for="kUI_dropdown_basic_select">Select a user</label>
    <select id="kUI_dropdown_basic_select" class="uk-form-width-medium">
        <?php  foreach ($response->result->allAcc as $data) { ?>
            <option  value="<?php echo $data->Id;?>">   <?php echo $data->Name; ?></option>
        <?php } ?>
    </select>
</form>

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>

<script type="text/javascript">
$(document).ready(function(){
  $("#kUI_dropdown_basic_select").change(function(){
        window.open( 'admin_mailbox.php?Id=' + $(this).val() ,"_self");
  });
});
</script>

1 Comment

You've now introduced jquery into the solution, which needlessly complicates the matter. You do not raise any attention to the fact that you've introduced dependencies with this solution, and it would work quite well with vanilla javascript, as noted by @Mr. Engineer's answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.