Categories
JavaScript Answers

How to increment number by 0.01 in JavaScript using a loop?

Sometimes, we want to increment number by 0.01 in JavaScript using a loop.

In this article, we’ll look at how to increment number by 0.01 in JavaScript using a loop.

How to increment number by 0.01 in JavaScript using a loop?

To increment number by 0.01 in JavaScript using a loop, we can use the JavaScript number’s toFixed method.

For instance, we write:

const heights = [];
for (let i = 1.20, l = 2.5; i < l; i += 0.01) {
  heights.push(i.toFixed(2));
}
console.log(heights)

We add a for loop that increments i by 0.01 with each iteration.

Then we call i.toFixed with 2 to round i to 2 decimal places and convert it to a string.

Then we call height.push to append the string to the array.

Conclusion

To increment number by 0.01 in JavaScript using a loop, we can use the JavaScript number’s toFixed method.

Categories
JavaScript Answers

How to get an element’s tag name in JavaScript?

Sometimes, we want to get an element’s tag name in JavaScript.

In this article, we’ll look at how to get an element’s tag name in JavaScript.

How to get an element’s tag name in JavaScript?

To get an element’s tag name in JavaScript, we can use an element’s nodeName property.

For instance, we write:

<div>

</div>

to add a div.

Then we write:

console.log(document.querySelector('div').nodeName)

to select the div with document.querySelector.

And use the nodeName property to return the node’s tag name as a string.

As a result, we should see 'DIV' logged.

Conclusion

To get an element’s tag name in JavaScript, we can use an element’s nodeName property.

Categories
JavaScript Answers

How to add HTML datalist values from array with JavaScript?

Sometimes, we want to add HTML datalist values from array with JavaScript.

In this article, we’ll look at how to add HTML datalist values from array with JavaScript.

How to add HTML datalist values from array with JavaScript?

To add HTML datalist values from array with JavaScript, we can create option elements with document.createElement.

For instance, we write:

<input name="fruit" list="list" />
<datalist id="list"></datalist>

to add the input and datalist element for the input.

Then we write:

const fruits = ['apple', 'orange'];
const list = document.getElementById('list');

for (const f of fruits) {
  const option = document.createElement('option');
  option.value = f;
  list.appendChild(option);
};

We get the input with document.getElementById.

Then we loop through each element in fruits with a for-of loop.

In the loop, we call document.createElement to create an option element.

Then we set the option.value property’s value to f.

Next, we call list.appendChild with option to add the option element to the datalist element.

Conclusion

To add HTML datalist values from array with JavaScript, we can create option elements with document.createElement.

Categories
JavaScript Answers

How to make a table row clickable with JavaScript?

Sometimes, we want to make a table row clickable with JavaScript.

In this article, we’ll look at how to make a table row clickable with JavaScript.

How to make a table row clickable with JavaScript?

To make a table row clickable with JavaScript, we can use jQuery.

For instance, we write:

<table>
  <tbody>
    <tr>
      <td><a href="#">Column 1</a></td>
      <td><a href="#">Column 2</a></td>
      <td><a href="#">Column 3</a></td>
    </tr>
  </tbody>
</table>

to add a table.

Then we write:

.highlightRow {
  background-color: green
}

to set background color for the highlightRow class.

Next, we write:

$('tr').click(function() {
  $(this).toggleClass('highlightRow');
});

to add an event handler for tr element clicks.

When we click on them, we call the toggleClass method on the tr element to toggle the highlightRow class.

Conclusion

To make a table row clickable with JavaScript, we can use jQuery.

Categories
JavaScript Answers

How to insert hyphens into a JavaScript string?

Sometimes, we want to insert hyphens into a JavaScript string.

In this article, we’ll look at how to insert hyphens into a JavaScript string.

How to insert hyphens into a JavaScript string?

To insert hyphens into a JavaScript string, we can use the JavaScript string’s replace method.

For instance, we write:

const phone = "1234567890";
const newPhone = phone.replace(/(\d{3})(\d{3})(\d+)/, '$1-$2-$3');
console.log(newPhone)

We call phone.replace with a regex that has 3 capturing groups for capturing 2 groups of 3 digits and the remaining digits respectively.

Then we put dashes in between each group with '$1-$2-$3'.

Therefore, newPhone is '123-456-7890'.

Conclusion

To insert hyphens into a JavaScript string, we can use the JavaScript string’s replace method.