canintex
canintex

Reputation: 638

Replace dropdown list with unordered list. The javascript way

I have

<div id="edit-country-wrapper">
  <select id="edit-country" class="form-select" name="country">
    <option selected="selected" value="all">All</option>
    <option value="canada">Canada</option>
    <option value="usa">USA</option>
  </select>
</div>

And want to change it to

<div id="edit-country-wrapper">
  <ul id="edit-country" class="form-select" name="country">
    <li><a class="selected" href="/all">All</a></li>
    <li><a class="" href="/canada">Canada</a></li>
    <li><a class="" href="/usa">USA</a></li>
  </ul>
</div>

This is what I have right now.

$(document).ready(function() {
    $('#edit-country-wrapper select').each(function() {
        var $select = $('<div id="location-select" />');
        $(this).find('option').each(function() {
            var $option = $('<a />');
            $option.attr({
                href: '?country=' +$(this).attr('value'),
                id: 'location-' + $(this).attr('value'),
                class: $(this).attr('selected'),
                html: $(this).html()
            });
            $select.append($option);
        });

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

I'm trying to replace a dropdown list with an unordered list. I have it working in FF & Chrome but not in IE. Please Help!!

Upvotes: 1

Views: 911

Answers (2)

Andy
Andy

Reputation: 30135

$option.attr({
  href: '?country=' +$(this).attr('value'),
  id: 'location-' + $(this).attr('value'),
  class: $(this).attr('selected'),
}).html($(this).html());

Upvotes: 1

alex
alex

Reputation: 490253

I don't think you can use attr to set HTML like that (I think it would just make a new attribute on the link called html).

Try replacing that with...

var $option = $('<a />', {
                href: '?country=' +$(this).attr('value'),
                id: 'location-' + $(this).attr('value'),
                class: $(this).attr('selected'),
                html: $(this).html()
            });

So long as you are using jQuery >= 1.4

Upvotes: 1

Related Questions