Bootstrap button group pre-select button with html only - html

Using Bootstrap I want a button group but with one button preselected. If I use the html below then the first button gets preselected, but it remains active even when I click on one of the other buttons.
Using only html how can I define the button group with one button selected where that button will deselect when I click on one of the other buttons.
<div class="btn-group">
<button type="button" class="btn btn-default active">Left</button>
<button type="button" class="btn btn-default">Middle</button>
<button type="button" class="btn btn-default">Right</button>
</div>

It's not completely possible to do this with HTML only - (as your title implies).
Really, the only thing you could do without JavaScript is add the attribute autofocus="autofocus" to the desired element like this:
<button type="button" class="btn btn-default" autofocus="autofocus">Left</button>
As the attribute implies, the button will automatically be focused in supported browsers. If another button is clicked, the focus of the first button will be removed.
EXAMPLE HERE
It's worth noting that the styling of .btn-default.active is the same as .btn-default:focus:
.btn-default:focus, .btn-default.active {
color: #333;
background-color: #ebebeb;
border-color: #adadad;
}
Unfortunately, the focus will also be removed when clicking anywhere else on the page too. If this is a problem, JavaScript would be needed to solve this. The solution would be to have the active class on the desired element and then remove it when clicking on any of the sibling button elements. The selection would be mutually exclusive like with radio elements.
Here is an example taken directly from the Bootstrap JS documentation. It's worth noting that the Bootstrap JS file/jQuery need to be included.
EXAMPLE HERE
<div class="btn-group" data-toggle="buttons">
<label class="btn btn-primary active">
<input type="radio" name="options" id="option1"> Option 1
</label>
<label class="btn btn-primary">
<input type="radio" name="options" id="option2"> Option 2
</label>
<label class="btn btn-primary">
<input type="radio" name="options" id="option3"> Option 3
</label>
</div>

Related

How can i delete space between labels in html?

So I have this code:
<form action="{{ url_for('profile') }}" method="post" enctype=multipart/form-data>
<label for="files" class="btn btn-light btn-sm">Cambiar foto de perfil</label>
<input id="files" style="visibility:hidden;" name="photo" type="file" required>
<input type="file" accept="image/*">
<label for="submit" class="btn btn-light btn-sm">Actualizar</label>
<button style="visibility:hidden;" id="submit" type="submit"> Actualizar </button>
</form>
I would like to know how to eliminate the space between these labels. I have tried with Margin="0" and nothing happened.....Thanks in advance.
Are you referring to the space to the right of "Cambiar foto de perfil"? If so, that's due to the adjacent input being styled with visibility: hidden. If you want that input to be hidden AND not take up any space, you would need to set it to Display: none instead of the visibility property.
Not quite sure what you want to achieve - assuming you want to have a form with 3 lines of input widgets, 2 file uploaders and a submit button, you may want to start off with something along these lines:
<form action="{{ url_for('profile') }}" method="post" enctype=multipart/form-data>
<div>
<label for="files" class="btn btn-light btn-sm">Cambiar foto de perfil:</label>
<input id="files" style="visibility:visible;" name="photo" type="file" required>
</div>
<div>
<div>
<label for="images" class="btn btn-light btn-sm">Cambiar el fichero del fotos:</label>
<input id="images" type="file" accept="image/*">
</div>
<div>
<label for="submit" class="btn btn-light btn-sm">Actualizar:</label>
<button style="visibility:visible;" id="submit" type="submit"> Actualizar </button>
</div>
</form>
Modifications in above code snippet:
Wrap each widget and its label in a div element.
Make the widgets visible
Add a label for the image uploader
Further suggestions:
Add a full-fledged css layout (flex or grid) to align widgets and labels.
Add Css styles to the divs and toggle the display property to selectively add or remove widgets ( beware, usually considered bad user interface design ).
The MDN Web Docs (previously Mozilla Developer Network, MDN) is an excellent resource for learning about web technologies. You may want to hav a look at the CSS section, in particular the CSS layout tutorial.

How to make a checkbox into a button?

Here is the code. There is a search bar where a user can put in a topic then select a button that shows different categories.
<form action="search.php" method="GET">
<i class="fas fa-search" aria-hidden="true"></i>
<input placeholder="Search" name="search" aria-label="Search">
<input type="submit" value="S" class="searchButton" name="submit-request"/>
<div class="buttons" style="text-align:center;">
<input type="button" class="spec" name="category1" value="category1">
<input type="button" class="spec" name="category2" value="category2">
<input type="button" class="spec" name="category3" value="category3">
<input type="button" class="spec" name="category4" value="category4">
<input type="button" class="spec" name="category5" value="category5">
</div>
</form>
This search form works perfectly fine with checkboxes. What should I do as an alternative? Is there a way I can style the checkboxes as buttons. If they are checkboxes how can I make it so that a user can only pick one category.
Use radio buttons.
Show name for radio with label.
To style a radio button, hide it, and add pseudo element.
<button> is it's own tag.
<button class="spec" name="category5">category5</button>
If you wanted to use CSS (you didn't tag it in your post), you can try this:
https://www.w3schools.com/css/tryit.asp?filename=trycss_form_button
<div class="navigation">
<input type="checkbox" class="navigation__checkbox" id="**navi-toggle**">
<label for="**navi-toggle**" class="navigation__button">
<span class="navigation__icon"> </span>
</label>
</div>
.navigation {
&__checkbox {
display: none;
}
&__button{
background-color: $color-white;
height: 7rem;
width: 7rem;
}
You say that you want to be able to still use checkbox as that is what functions... Well you could do so and use a label that is linked to that checkbox, and then simply style your label as your button.

how to use type="color" in <button> tag

I need to use a button tag with a color picker , but I can't use input because the value can't be a google material icon
what can I do?
<button type="color" value="#ff0000" type="button" class="btn btn-secondary herramienta"><i class="material-icons">format_color_text</i></button>
You would probably have an input with opacity set to 0, put it on top of the button, and then when they go to click the button, they actually click on the input, something like:
<div style="position:relative; display:inline-block">
<button type="button" class="btn btn-secondary herramienta"><i class="material-icons">format_color_text</i></button>
<input type="color" value="#ff0000" style="opacity:0; position:absolute; left:0;top:0;width:100%"/>
</div>
https://jsfiddle.net/3u8v5axs/

All buttons in <form> are posting

I have a form with multiple textboxes and buttons. I want to use some of the buttons to clear the textbox. But when I click on the button Clear text the form gets posted.
Does someone know how I can fix this?
Here is my HTML:
<form name="reaction" method="post" action="./send.php">
<input type="text" name="firstname">
<button class="btn btn-default" onclick="ClearText1();">Clear text</button><br />
<input type="text" name="lastname">
<button class="btn btn-default" onclick="ClearText2();">Clear text</button><br />
<button class="btn btn-block btn-success">Send</button>
</form>
use <button type="submit" for submit button and <button type="button" for other button.You can read More about button here.
From MDN
The type attribute of the button. Possible values are:
submit: The button submits the form data to the server. This is the default if the attribute is not specified, or if the attribute is dynamically changed to an empty or invalid value.
reset: The button resets all the controls to their initial values.
button: The button has no default behavior. It can have client-side scripts associated with the element's events, which are triggered when the events occur.
menu: The button opens a popup menu defined via its designated element.
<form name="reaction" method="post" action="./send.php">
<input type="text" name="firstname">
<button type="button" class="btn btn-default" onclick="ClearText1();">Clear text</button><br />
<input type="text" name="lastname">
<button type="button" class="btn btn-default" onclick="ClearText2();">Clear text</button><br />
<button type="submit" class="btn btn-block btn-success">Send</button>
</form>
Use event.preventDefault(); to prevent default submit behavior for this button.
<script>
function ClearText2(event){
// your code....
event.preventDefault();
}
</script>

Bootstrap Radio button : radio circle removal

I am trying to use Bootstrap for my website. I have radio buttons and I am trying to use "Buttons" from bootstrap for the same.
<td style="margin-bottom:0px; padding-bottom: 0px;font-size=12px;vertical-align:bottom;">
<div class="btn-group" data-toggle="buttons" id="topButtonDiv" >
<button type="button" class="btn btn-primary">Home
<input type="radio" id="radio1" ></button>
<button type="button" class="btn btn-primary">Home1
<input type="radio" id="radio2" > </button>
<button type="button" class="btn btn-primary">Home2
<input type="radio" id="radio7"> </button>
</div>
</td>
The problem I am facing is that I still see the circles in the Radio button present, where as in the Bootstrap example, I see no such circles present.
http://getbootstrap.com/javascript/#buttons-usage
Can you let me know what I am missing here?
Another alternative if the css version update is not working is to manually hide the radio button using css
[type='radio'] {
display: none;
}
Check for the version of css you've added. The same btn-group class works fine here
Your markup for bootstrap radio buttons is wrong, you make it like this:
<div class="radio">
<label>
<input type="radio" name="optionsRadios" id="optionsRadios1" value="option1" checked>
Option one is this and that—be sure to include why it's great
</label>
</div>
Furthermore you can't put a input element within a button element. That's invalid html.