Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to convert a select menu to buttons?

I have a simple select menu e.g.

<p>Would you recommend this company?</p>

<select>
    <option value="yes">Yes</option>
    <option value="no">No</option>
</select>

That I ask for user feedback, it's currently power by a simple form submit, now it's 2013 i'd love to make it 2 x buttons e.g. "Thumbs Up" or "Thumbs Down" (And highlights when selected)/interchangable (e.g. can't select 2 at once).

Got this far... But it's not really applying the select values or a "selected" effect:

http://jsfiddle.net/mBUgc/18/

Closest i've come to is the following...which converts select menus to star ratings... - But it's not really a usable, so hopefully someone can help/point in right direction or a little code example.

http://netboy.pl/demo/jquery-bar-rating/examples/

like image 257
user2736203 Avatar asked Sep 21 '13 10:09

user2736203


People also ask

How do I create a custom select menu?

Create a Custom Select Menu 1 Add HTML: Example &lt;!-- Surround the select box within a "custom-select" DIV element. ... 2 Add CSS: Example /* The container must be positioned relative: */ .custom-select { position: relative; font-family: Arial; } .custom-select select { display: none; /*hide original SELECT element: */ ... 3 Add JavaScript:

How to change the text of a button on the screen?

First screen, - Add a button, and change the Text = mytext <---, you are setting the Text property becomes a Context Variable (it shows error, is ok, leave it, it will disappear at step 2) - Screen1.OnVisible = UpdateContext ( {mytext: "Show Menu"}) <-- so, when you navigate from Screen2 to Screen1

How do I show or hide the'show menu'button?

The ' Show Menu ' button text, changes from ' Show Menu' to ' Hide Menu .' Assuming the above, is not possible. An alternative might be when a user taps ' Show Menu ' button the button is hidden and the ' Hide Menu ' button displays.

How do I show the menu on screen 1?

When screen1 on visible, the Button will shwo "Show Menu". - click again, and since it is now "Hide Menu", it will change to "Show Menu". The same method can be used in anyway.


2 Answers

Update:

Hidden element added for form submission of chosen value.

http://jsfiddle.net/mBUgc/21/


Here's a simple implementation of what you are looking to achieve.

Demo: http://jsfiddle.net/mBUgc/19/

JavaScript:

$("select option").unwrap().each(function() {
    var btn = $('<div class="btn">'+$(this).text()+'</div>');
    if($(this).is(':checked')) btn.addClass('on');
    $(this).replaceWith(btn);
});

$(document).on('click', '.btn', function() {
    $('.btn').removeClass('on');
    $(this).addClass('on');
});

CSS:

div.btn {
    display: inline-block;
    /** other styles **/
}
div.btn.on {
    background-color: #777;
    color: white;
    /** styles as needed for on state **/
}

Note: This will work regardless of the number of options you have in your select - http://jsfiddle.net/mBUgc/20/

like image 177
techfoobar Avatar answered Sep 28 '22 22:09

techfoobar


In case someone comes back to this, I came up with a slight different solution based on the example from @techfoobar.

basically: - dynamic code (can be used for multiple selects on the same page) - no need of hidden fields, select is still there (hidden)

tested: - current chrome, firefox, edge (chromium), ie11

Usage:

jQuery(document).ready(function($) {
  ReplaceSelectWithButtons($('#test'));
});

https://jsfiddle.net/koshimon/pobcvxf4/2/

// yay, this is my first post here, feedback welcome :)

like image 38
Koshimon Avatar answered Sep 28 '22 22:09

Koshimon