2

I have a dropdown-menu that I auto populate base on my Ajax call. I want to redirect to specific URL on select change.


jQuery

// Auto Populate the dropdown-menu
$("#dd").append('<option value="' + userId + '"> <a href="">' + name + '</a></option>');

Thanks

code-8
  • 54,650
  • 106
  • 352
  • 604
  • 3
    Because it isn't valid HTML markup, don't expect it to work. Explain isntead why would you need that? Sounds like on `select`change, redirect to specific URL – A. Wolff Jul 20 '15 at 20:14
  • short answer is no...it;s invalid html and doesn't make any sense either – charlietfl Jul 20 '15 at 20:15
  • What did I missed there ? I couldn't tell. Hope u don't mind point it out. – code-8 Jul 20 '15 at 20:15
  • anchor tags cannot go inside option tags – depperm Jul 20 '15 at 20:16
  • While this is invalid HTML (for the reasons stated), there are non-native/emulated drop-down lists (found in various UI libraries) that support all sort of extras including rich content. – user2864740 Jul 20 '15 at 20:16
  • @A.Wolff : "Sounds like on selectchange, redirect to specific URL" - you're right, that is exactly, what I am trying to do – code-8 Jul 20 '15 at 20:16
  • Just read the spec: http://www.w3.org/TR/html-markup/option.html – A. Wolff Jul 20 '15 at 20:16
  • Create a custom dropdown component to support html in options, in native select its not allowed. – vinayakj Jul 20 '15 at 20:16
  • @vinayakj : Thank-you for the tip. I didn't know that. – code-8 Jul 20 '15 at 20:17
  • @A.Wolff : selectchange, redirect to specific URL , can you show me how to do that ? – code-8 Jul 20 '15 at 20:18
  • `onchange= location.href=this.value` – vinayakj Jul 20 '15 at 20:18
  • @开发人员 Instead close/delete this question and ask a new one – A. Wolff Jul 20 '15 at 20:19
  • 1
    When you insert an HTML element inside an element where it shouldn't be, the browser will try to fix the structure so that it will display the way it was intended. Similarly if you insert a `

    ` inside a `

    ` the browser will remove the `

    ` and place it outside.

    – Jackson Jul 20 '15 at 20:19
  • tags cannot contain any other tags. If you want to do it then you need to used other plugin which will create a wrapper on your select dropdown like select2 plugin – abs Jul 20 '15 at 20:20
  • you can refer http://stackoverflow.com/questions/2000656/using-href-links-inside-option-tag – ashokd Jul 20 '15 at 20:21
  • For every item (option) for value put Your URL location and `onchange="window.location=this.value"` – nelek Jul 20 '15 at 20:22
  • @nelek : Can you please link your suggestion with part of my code ? – code-8 Jul 20 '15 at 23:07
  • @abs : I update my post, can you help me make it happen ? – code-8 Jul 20 '15 at 23:08

1 Answers1

1

Would recommend reading about HTML tags.

<select id="mySelect" onchange="myFunction()">

function myFunction() {
  //Redirect to URL 
}

Ready more at onchange Event, JavaScript Window Location

Cheers !

Sachin Thapa
  • 3,559
  • 4
  • 24
  • 42
  • Do I need to do anything else in my `option` tag ? Can you be a little more details ? – code-8 Jul 20 '15 at 23:05
  • Function will be invoked whenever you change the selected value. You need to write logic in function, you may want to read selected value. – Sachin Thapa Jul 21 '15 at 14:40