How to add tooltips for button elements with HTML?

Sometimes, we want to add tooltips for button elements with HTML.

In thjis article, we’ll look at how to add tooltips for button elements with HTML.

How to add tooltips for button elements with HTML?

To add tooltips for button elements with HTML, we add the title attribute.

For instance, we write

<button title="Hello World!">Sample Button</button>

to add the title attribute and set the tooltip text ‘Hello World!’ to the button.

Then when we hover over it, ‘Hello World!’ is displayed in the tooltip.

Conclusion

To add tooltips for button elements with HTML, we add the title attribute.

How to make a text input non-editable with HTML?

Sometimes, we want to make a text input non-editable with HTML.

In this article, we’ll look at how to make a text input non-editable with HTML.

How to make a text input non-editable with HTML?

To make a text input non-editable with HTML, we can set the readonly attribute.

For instance, we write

<input type="text" value="3" class="field left" readonly="readonly" />

to set the readonly`` attribute to readonly` on the input to stop users from editing its value.

Conclusion

To make a text input non-editable with HTML, we can set the readonly attribute.

How to make a vertical line in HTML?

Sometimes, we want to make a vertical line in HTML.

In this article, we’ll look at how to make a vertical line in HTML.

How to make a vertical line in HTML?

To make a vertical line in HTML, we can add some styles to the hr element.

For instance, we werite

<hr width="1" size="500" />

to set the hr element’s width and size to make it vertical.

If the width is less than its size, then we see a vertical line.

Conclusion

To make a vertical line in HTML, we can add some styles to the hr element.

How to set the selected option of the select box with JavaScript?

Sometimes, we want to set the selected option of the select box with JavaScript.

In this article, we’ll look at how to set the selected option of the select box with JavaScript.

How to set the selected option of the select box with JavaScript?

To set the selected option of the select box with JavaScript, we can use the setAttribute method.

For instance, we write

<select id="gate">
  <option value="null">- choose -</option>
  <option value="Gateway 1">Gateway 1</option>
  <option value="Gateway 2">Gateway 2</option>
</select>

to add the select element.

Then we write

document
  .querySelector("#gate option[value='Gateway 2']")
  .setAttribute("selected", true);

to select the option element we want to select with querySelector.

And we call setAttribute with 'selected' and true to select the option.

Conclusion

To set the selected option of the select box with JavaScript, we can use the setAttribute method.

How to fill the remaining height or width in a flex container with CSS?

Sometimes, wee want to fill the remaining height or width in a flex container with CSS.

In this article, we’ll look at how to fill the remaining height or width in a flex container with CSS.

How to fill the remaining height or width in a flex container with CSS?

To fill the remaining height or width in a flex container with CSS, we use flexbox.

For instance, we write

<div class="row">
  <div class="left">Left</div>
  <div class="separator"></div>
  <div class="right">Right With Text</div>
</div>
<div class="row">
  <div class="left">Left With More Text</div>
  <div class="separator"></div>
  <div class="right">Right</div>
</div>
<div class="row">
  <div class="left">Left With Text</div>
  <div class="separator"></div>
  <div class="right">Right With More Text</div>
</div>

to add some nested divs.

Then we write

.row {
  background: lightgray;
  height: 30px;
  width: 100%;
  display: flex;
  align-items: flex-end;
  margin-top: 5px;
}
.left {
  background: lightblue;
}
.separator {
  flex-grow: 1;
  border-bottom: dotted 2px black;
}
.right {
  background: coral;
}

to make the div with class row a flex container with display: flex;.

And we make the divs with class separator fill the remaining space with flex-grow: 1;.

Conclusion

To fill the remaining height or width in a flex container with CSS, we use flexbox.

How to get a div height with plain JavaScript?

Sometimes, we want to get a div height with plain JavaScript.

In this article, we’ll look at how to get a div height with plain JavaScript.

How to get a div height with plain JavaScript?

To get a div height with plain JavaScript, we use the clientHeight or offsetHeight property.

For instance, we write

const clientHeight = document.getElementById("myDiv").clientHeight;
const offsetHeight = document.getElementById("myDiv").offsetHeight;

to select the div with getElementById.

Then we get its height with clientHeight or offsetHeight.

clientHeight includes padding.

And offsetHeight includes the heights of the padding, scrollBar and borders.

Conclusion

To get a div height with plain JavaScript, we use the clientHeight or offsetHeight property.

How to adjust the width and height of an iframe to fit with content in it with JavaScript?

Sometimes, we want to adjust the width and height of an iframe to fit with content in it with JavaScript.

In this article, we’ll look at how to adjust the width and height of an iframe to fit with content in it with JavaScript.

How to adjust the width and height of an iframe to fit with content in it with JavaScript?

To adjust the width and height of an iframe to fit with content in it with JavaScript, we can set the iframe’s parent window’s dimensions when the iframe’s content is loading.

For instance, we write

window.onload = () => {
  parent.document.getElementById("fileUploadIframe").style.height =
    document.body.clientHeight + 5 + "px";
  parent.document.getElementById("fileUploadIframe").style.width =
    document.body.clientWidth + 18 + "px";
};

to select the parent window’s iframe from the iframe’s code with parent.document.getElementById("fileUploadIframe").

Then we set its height and width to the iframe’s body element’s clientHeight and clientWidth.

Conclusion

To adjust the width and height of an iframe to fit with content in it with JavaScript, we can set the iframe’s parent window’s dimensions when the iframe’s content is loading.

How to allow 2 decimal places in number inputs with HTML?

Sometimes, we want to allow 2 decimal places in number inputs with HTML.

In this article, we’ll look at how to allow 2 decimal places in number inputs with HTML.

How to allow 2 decimal places in number inputs with HTML?

To allow 2 decimal places in number inputs with HTML, we can set the step attribute.

For instance, we write

<input type="number" step="0.01" class="form-control" />

to set the step attribute to 0.01 to allow 2 decimal places to be entered into the number input.

Conclusion

To allow 2 decimal places in number inputs with HTML, we can set the step attribute.

How to get the ID of the clicked button on click with JavaScript?

Sometimes, we want to get the ID of the clicked button on click with JavaScript.

In this article, we’ll look at how to get the ID of the clicked button on click with JavaScript.

How to get the ID of the clicked button on click with JavaScript?

To get the ID of the clicked button on click with JavaScript, we can get the id property of the element clicked.

For instance, we write

<button id="1">B1</button>
<button id="2">B2</button>
<button id="3">B3</button>

to add 3 buttons.

Then we write

document.onclick = (e) => {
  console.log(e.target.id);
};

to set document.onclick to a function that’s called when we click on anything on the page.

And we can get ID of the element that’s clicked with e.target.id.

Conclusion

To get the ID of the clicked button on click with JavaScript, we can get the id property of the element clicked.

How to HTML5 fix dragleave event firing when hovering a child element with JavaScript?

Sometimes, we want to HTML5 fix dragleave event firing when hovering a child element with JavaScript.

In this article, we’ll look at how to HTML5 fix dragleave event firing when hovering a child element with JavaScript.

How to HTML5 fix dragleave event firing when hovering a child element with JavaScript?

To HTML5 fix dragleave event firing when hovering a child element with JavaScript, we call addEventListener with false.

For instance, we write

const dropZone = document.getElementById("box");
const dropMask = document.getElementById("drop-mask");

dropZone.addEventListener("dragover", dragOver, false);
dropMask.addEventListener("dragleave", dragLeave, false);
dropMask.addEventListener("drop", dragDrop, false);

to select the drop zone element with

const dropZone = document.getElementById("box");

We select the draggable element with

const dropMask = document.getElementById("drop-mask");

Then we call addEventListener with false to disable event capturing which propagates events from the ancestors element to the originating element.

Then the dragleave event won’t be fired when hovering a child element.

Conclusion

To HTML5 fix dragleave event firing when hovering a child element with JavaScript, we call addEventListener with false.