Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to build a dropdown select menu similar to the category and location dropdowns of Gumtree:

www.gumtree.co.za

The following function seems to perform the general idea, using data-value to pass values to each li (http://jsfiddle.net/Starx/a6NJk/2/[^]):

HTML:

HTML
<a href="#"A id="submit"> Get Value</a>
<ul>
    <li class="init">[SELECT]</li>
    <li data-value="value 1">Option 1</li>
    <li data-value="value 2">Option 2</li>
    <li data-value="value 3">Option 3</li>
</ul>


JS:

JavaScript
$("ul").on("click", ".init", function() {
    $(this).closest("ul").children('li:not(.init)').toggle();
});

var allOptions = $("ul").children('li:not(.init)');
$("ul").on("click", "li:not(.init)", function() {
    allOptions.removeClass('selected');
    $(this).addClass('selected');
    $("ul").children('.init').html($(this).html());
    allOptions.toggle();
});


$("#submit").click(function() {
    alert("The selected Value is "+ $("ul").find(".selected").data("value"));
});


How would I use the name attribute though, to substitute for:

HTML
<select name="example">


Any help will be greatly appreciated!
Posted
Comments
Palash Mondal_ 5-Oct-15 7:42am    
Your issue is bit not clear here. Can you explain what you want to do with the name attribute?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900