Quick Add Html Button with CSS3 and JQuery<!-- --> | <!-- -->Patrick Desjardins Blog
Patrick Desjardins Blog
Patrick Desjardins picture from a conference

Quick Add Html Button with CSS3 and JQuery

Posted on: August 4, 2015

This is a small JavaScript and CSS control that allows to have some edition from a textbox in a fashion that this one is only a button to minimize space and once clicked become a more extended version. The goal is to have a way to add information in a quick way without taking too much space. This is perfect for situation where adding information is not the main task. The use case is that a user want to add information which require a text box. The user click the button to open the edition which reveal a text box. The button does not need to stay there because we want to save space so the button transforms into a closing button which cancel the operation if required. When the user starts to type, a save button is added. From there, the user can click the cancel button or hit save. In both case, the text box is rolling back and the user interface come back to the initial state. This design is intended to be minimal. There is not label. This is why we need to use a watermark, also known as placeholder in the text box. Since we want to limit button, the expand button and cancel (collapse) button is the same. To make sure that the user knows what the button is doing we display a tooltip but also we have an animation that transform visually the button to its intend.

To keep it simples, let's start with a division that will hold all our Html elements.

1<div id="container"> </div>

Next, we need to create our three main controls. The first one is the input where the user will write any information that will be saved. The second one is the toggle button that expand or collapse the input box and the third one is the save button that will be displayed only when the user save.

1var $controlContainer = $('#container');
3var $inputBox = $("<input>")
4 .attr('id', 'inline-editor-textbox')
5 .attr('type', 'text') .attr('value', '')
6 .attr('maxlength', 20 )
7 .attr('style', 'display:none')
8 .attr('placeholder', "This is some watermark")
9 .addClass('hide-input-inline') ;
12var $buttonToggle = $("<button>")
13 .attr('id', 'toggleButton')
14 .attr('title', 'Open')
15 .attr('type', 'button');
16var $spanIcon = $('<span>')
17 .addClass("glyphicon glyphicon-plus");
22var $buttonOk = $("<button>")
23 .attr('id', 'okButton')
24 .attr('style', 'display:none')
25 .attr('title', 'Ok')
26 .addClass('hide-ok-button')
27 .attr('type', 'button');
29var $spanIconOk = $('<span>')
30 .addClass("glyphicon glyphicon-save");

The next step is to add events. The toggle will open and close the input. It will also change the icon of the button from a + to a x. The okay button will save the information and collapse the input if everything is fine. The keyup is there to show the save button if the text box contains text.

1$buttonToggle.click({ input: $inputBox, buttonToggle: $buttonToggle, buttonOk: $buttonOk},onToggle);
2$buttonOk.click({ input: $inputBox, buttonToggle: $buttonToggle, buttonOk: $buttonOk},onOk);
3$inputBox.keyup( { input: $inputBox, buttonOk: $buttonOk }, onKeyUp);

The toggle function has reference to some of our controls. This is a great way to pass reference without having to communicate through JQuery selector. Depending of the state of the toggle, which is the class "hide-input-inline" we make it appears or not. We add a delay because we have CSS3 animation and we want everything to be smooth together.

1function onToggle(jqueryEvent) {
2 var data = jqueryEvent.data;
3 var $input = data.input;
4 var $buttonToggle = data.buttonToggle;
5 var $buttonOk = data.buttonOk;
7var isHidden = $input.hasClass("hide-input-inline");
8if (isHidden) {
9 $input.fadeIn();
10 $input.removeClass("hide-input-inline");
11 $input.addClass("show-input-inline");
12 $input.val('');
13 $buttonToggle.addClass("add-to-close");
14 $buttonToggle.attr('title', 'Close');
15} else {
16 $input.removeClass("show-input-inline");
17 $input.addClass("hide-input-inline")
18 .delay(600)
19 .fadeOut();
20 $buttonToggle.removeClass("add-to-close");
21 $buttonOk.addClass("hide-ok-button");
22 $buttonOk.removeClass("show-ok-button").delay(600).fadeOut();
23 $buttonToggle.attr('title', 'Open');
24 }

The key up is to check the length but we could also check for "Enter" to be pressed and invoke the Ok code. The code is adding or removing classes, with a delay to let CSS animation to be executed.

1function onKeyUp(jqueryEvent) {
2 var data = jqueryEvent.data;
3 var $input = data.input;
4 var $buttonOk = data.buttonOk;
5 if ($input.val().length === 0) {
6 $buttonOk.removeClass("show-ok-button");
7 $buttonOk.addClass("hide-ok-button").delay(600).fadeOut();
8 htmlElementWithIconState($buttonOk, true);
9 } else {
10 $buttonOk.fadeIn();
11 $buttonOk.addClass("show-ok-button");
12 $buttonOk.removeClass("hide-ok-button");
13 htmlElementWithIconState($buttonOk, false);
14 }

This helper method is used to mark the save icon into a disable state. At this moment, this is not really used but if validation is added we could still have the button add without having to remove it until the user fix the problem.

1function htmlElementWithIconState($element, disabled) {
2 $element.prop("disabled", disabled);
3 $element.find('.glyphicon').css('opacity', (disabled === true) ? .4 : 1);

Finally, we have the okay button hit which close up the text box and every buttons. In real life, a callback into a real save or okay code would be executed. If successful than close, otherwise keep everything open for the user to adjust his or her input.

1function onOk(jqueryEvent) {
2 var data = jqueryEvent.data;
3 var $input = data.input;
4 var $buttonOk = data.buttonOk;
5 onToggle(jqueryEvent);
1#toggleButton, #okButton {
2 padding:0 2px 4px 2px;
3 margin-left:3px;
4 height: 22px;
5 width: 22px;
6 vertical-align: top;
9#toggleButton span.glyphicon {
10 transition: all 1000ms ease;
13#okButton {
14 transition: all 1000ms ease;
17.hide-input-inline {
18 width:0;
19 transition: width 1000ms ease-in-out;
22.show-input-inline {
23 width:210px;
24 transition: width 1000ms ease-in-out;
27.add-to-close span {
28 transform: rotate(135deg);
31.hide-ok-button {
32 opacity:0;
35.show-ok-button {
36 opacity:1;
39#inline-editor-textbox::-webkit-input-placeholder {
40 /* WebKit browsers */
41 color:#7f7f7f
44#inline-editor-textbox {
45 border: none;
46 background-color: #E2E2E2;
47 color:#7f7f7f;
48 padding-left:4px;
49 height:22px;/*To fix the size of the button since we do not have any border*/
50 }
51 }
53#inline-editor-textbox:focus { outline: none; background-color:white; }

Interactive Demo