views:

857

answers:

3

How do I convert an unordered list in this format:

<ul class="selectdropdown">
    <li><a href="one.html" target="_blank">one</a></li>
    <li><a href="two.html" target="_blank">two</a></li>
    <li><a href="three.html" target="_blank">three</a></li>
    <li><a href="four.html" target="_blank">four</a></li>
    <li><a href="five.html" target="_blank">five</a></li>
    <li><a href="six.html" target="_blank">six</a></li>
    <li><a href="seven.html" target="_blank">seven</a></li>
</ul>

into a dropdown in this format:

<select>
<option value="one.html" target="_blank">one</option>
<option value="two.html" target="_blank">two</option>
<option value="three.html" target="_blank">three</option>
<option value="four.html" target="_blank">four</option>
<option value="five.html" target="_blank">five</option>
<option value="six.html" target="_blank">six</option>
<option value="seven.html" target="_blank">seven</option>
</select>

using jQuery?

Edit:

in select box upon select item link should be open in new window and i want to give style to drop down link in this plugin http://www.dfc-e.com/metiers/multimedia/opensource/jqtransform/ i just need style for select dropdown

+6  A: 
$(function() {
    $('ul.selectdropdown').each(function() {
        var $select = $('<select />');

        $(this).find('a').each(function() {
            var $option = $('<option />');
            $option.attr('value', $(this).attr('href')).html($(this).html());
            $select.append($option);
        });

        $(this).replaceWith($select);
    });
});

EDIT

As with any jQuery code you want to run on page load, you have to wrap it inside $(document).ready(function() { ... }); block, or inside it's shorter version $(function() { ... });. I updated the function to show this.

EDIT

There was a bug in my code also, tried to take href from the li element.

Tatu Ulmanen
but it's not working http://jsbin.com/omulu
metal-gear-solid
still not working http://jsbin.com/uvabi
metal-gear-solid
You are trying to execute the script inside <style> tags, change those to <script>. I also updated my function, there was a small mistake.
Tatu Ulmanen
oh , my mistake , solved now now i added ur latest code it's working now http://jsbin.com/igano but on select a item from dropdown i wanted to open link in new window
metal-gear-solid
I updated my code in question
metal-gear-solid
@Jitendra, opening link on change and styling the select is out of the scope for this question, Google has a lot of resources for both of those. If the code is working, lets just close this one.
Tatu Ulmanen
Yes code is working and great thanks for it. I closed the question but if u can give the way to open links in new window then it would be better
metal-gear-solid
I found a another js solution for same http://www.onlinetools.org/tests/enhancedropdown.html and in this link opens in new window but the problem is it's not jquery based
metal-gear-solid
First you need to bind the created select element to a change event, and in that, get the value and redirect the browser. If you have problems with that, post a new question but try to do it yourself first.
Tatu Ulmanen
A: 
$('ul.selectdropdown').each(function(){

 var select=$(document.createElement('select')).insertBefore($(this).hide());

 $('>li a', this).each(function(){

  var a=$(this).click(function(){

   if ($(this).attr('target')==='_blank'){

    window.open(this.href);

   }

   else{

    window.location.href=this.href;

   }

  }),

     option=$(document.createElement('option')).appendTo(select).val(this.href).html($(this).html()).click(function(){

    a.click();

   });

  });

 });

In reply to your last comment, I modified it a little bit but haven't tested it. Let me know.

$('ul.selectdropdown').each(function(){
  var list=$(this),
  select=$(document.createElement('select')).insertBefore($(this).hide());
  $('>li a', this).each(function(){
    var target=$(this).attr('target'),
    option=$(document.createElement('option'))
     .appendTo(select)
     .val(this.href)
     .html($(this).html())
     .click(function(){
       if (target==='_blank'){
         window.open($(this).val());
       }
       else{
         window.location.href=$(this).val();
       }
      });
  });
  list.remove();
});
czarchaic
thx i'm trying ur solution
metal-gear-solid
no link not opening in new window http://jsbin.com/etoyu
metal-gear-solid
Is it not working or is it not what you want?
czarchaic
code working to make dropdown but links not opening in new window
metal-gear-solid
link opening in same tab in firefox
metal-gear-solid
It should only open a new window with a _blank target on the link. Have you dropped a console.log into the if statement to see if it's trying to open in a new window?
czarchaic
yes your code was right. it was my mistake i had not added target=_blank . now it working fine u can see here http://jsbin.com/agane thanks for this.
metal-gear-solid
but now the problem is unordered still showing in source code
metal-gear-solid
i tested ur new code but now select dropdown is gone
metal-gear-solid
Ok, found the error. Try it again please.
czarchaic
Cool it's perfect now. now i just want to give cool look to this <select> like this pluging does http://www.dfc-e.com/metiers/multimedia/opensource/jqtransform/
metal-gear-solid
A: 

Is there any way to achieve this WITHOUT the use of jQuery?

mark