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
Javascript
var selected = $(".huge.ui.buttons").find(".active").text();
$("#div_content").val(selected);