2

How do I make the Semantic UI Button Group selectable so that I can get the one that's selected in PHP?

Here is the button group:

<div class="huge ui buttons">
  <div class="ui button">One</div>
  <div class="ui button">Two</div>
  <div class="ui button">Three</div>
</div>

I would assume we are to treat the buttons like radio buttons where only one can be selected but Semantic UI doesn't really have any documentation on how to do the same with button groups.

How would I make the buttons selectable so I can send it to my PHP script?

EDIT:

Here is a jsFiddle showing how my button groups at the moment after trying to implement the toggle as shown here

Community
  • 1
  • 1
user3574492
  • 6,225
  • 9
  • 52
  • 105

1 Answers1

2

Whenever a button is pressed Semantic UI gives that button a class name called active.

To select from your group you could do:

var selected = document.querySelector(".huge.ui.buttons").querySelector(".active");

This will select the UI group with the first part and returns the selected element with the second part. You can perform actions on the selected element like selected.textContent. This will return the display text of the element.


Since you've tagged jQuery, I will provide a jQuery example too:

var selected = $(".huge.ui.buttons").find(".active");

NOTE: Please not that you will likely create multiple groups with the same class names (huge ui buttons). You will need to discriminate between them. Either use document.querySelectorAll and select the proper element using its index or give every group a unique ID so you can access the group with document.getElementById.

NOTE 2: Semantic isn't documenting this, but they use additional code on their page to set the button to active. You can find it here. How to toggle content in Semantic UI buttons?. I modified it to work with your Fiddle. http://jsfiddle.net/cgz7sv4g/2/


Addon to Semantic UI:

semantic = {};
// ready event
semantic.ready = function() {

  // selector cache
  var
    $buttons = $('.ui.buttons .button'),
    $toggle  = $('.main .ui.toggle.button'),
    $button  = $('.ui.button').not($buttons).not($toggle),
    // alias
    handler = {

      activate: function() {
        $(this)
          .addClass('active')
          .siblings()
          .removeClass('active')
        ;
      }

    }
  ;

  $buttons
    .on('click', handler.activate)
  ;


// attach ready event
$(document)
  .ready(semantic.ready)
;

This block of code will extend your Semantic Buttons with a handler that adds the class active to currently clicked element and removes it from other elements in the group. You can extend Semantic UI's elements with this code. using basic jQuery, since Semantic UI is a platform build upon jQuery.

EDIT:


To send the content of the div in through a form request

  • needed a hidden input

Javascript

 var selected = $(".huge.ui.buttons").find(".active").text();
 $("#div_content").val(selected);
Community
  • 1
  • 1
Mouser
  • 13,132
  • 3
  • 28
  • 54