Categories
JavaScript Answers

How to get the first item from a map with JavaScript?

Sometimes, we want to get the first item from a map with JavaScript.

In this article, we’ll look at how to get the first item from a map with JavaScript.

How to get the first item from a map with JavaScript?

To get the first item from a map with JavaScript, we can use the next method.

For instance, we write

const m = new Map();
m.set("key1", {});
m.set("keyN", {});

console.log(m.entries().next().value);

to create the map m.

Then we get an iterator with the key and value with entries.

And we call next to get the first entry.

We get its key value pair from the value property.

Also, we can get an iterator with the keys with keys.

console.log(m.keys().next().value);

And, we can get an iterator with the values with values.

console.log(m.values().next().value);

Conclusion

To get the first item from a map with JavaScript, we can use the next method.

Categories
JavaScript Answers

How to find if element with specific ID exists or not with JavaScript?

Sometimes, we want to find if element with specific ID exists or not with JavaScript.

In this article, we’ll look at how to find if element with specific ID exists or not with JavaScript.

How to find if element with specific ID exists or not with JavaScript?

To find if element with specific ID exists or not with JavaScript, we check if getElementById returns the element or null.

For instance, we write

const myEle = document.getElementById("myElement");
if (myEle) {
  const myEleValue = myEle.value;
}

to get the element with getElementById.

And then we get the value property of myEle if it’s not null.

Conclusion

To find if element with specific ID exists or not with JavaScript, we check if getElementById returns the element or null.

Categories
JavaScript Answers

How to define a regex pattern to match at least 1 number and 1 character in a string with JavaScript?

Sometimes, we want to define a regex pattern to match at least 1 number and 1 character in a string with JavaScript.

In this article, we’ll look at how to define a regex pattern to match at least 1 number and 1 character in a string with JavaScript.

How to define a regex pattern to match at least 1 number and 1 character in a string with JavaScript?

To define a regex pattern to match at least 1 number and 1 character in a string with JavaScript.

For instance, we write

const re = /^(?:[0-9]+[a-z]|[a-z]+[0-9])[a-z0-9]*$/i;

to match at least one number with [0-9]+.

And we match one character with [a-z].

i lets us match in a case insensitive manner.

Conclusion

To define a regex pattern to match at least 1 number and 1 character in a string with JavaScript.

Categories
JavaScript Answers

How to insert HTML at caret in a contenteditable div with JavaScript?

Sometimes, we want to insert HTML at caret in a contenteditable div with JavaScript.

In this article, we’ll look at how to insert HTML at caret in a contenteditable div with JavaScript.

How to insert HTML at caret in a contenteditable div with JavaScript?

To insert HTML at caret in a contenteditable div with JavaScript, we c an use the execCommand method.

For instance, we write

<iframe id="editor" src="contenu_editor_wysiwyg.php"> </iframe>

to add an iframe.

Then we write

const editor = document.getElementById("editor");
editor.contentDocument.designMode = "on";
editor.contentWindow.document.execCommand("insertHTML", false, "<br />");

to get the editor with getElementById.

And then we set designMode to 'on' so that we can insert HTML.

Then we insert the HTML at the caret of the iframe’s document by calling execuCommand with 'insertHTML' and the HTML to add.

Conclusion

To insert HTML at caret in a contenteditable div with JavaScript, we c an use the execCommand method.

Categories
JavaScript Answers

How to get the file path of the currently executing JavaScript code?

Sometimes, we want to get the file path of the currently executing JavaScript code.

In this article, we’ll look at how to get the file path of the currently executing JavaScript code.

How to get the file path of the currently executing JavaScript code?

To get the file path of the currently executing JavaScript code, we can use the document.currentScript property.

For instance, we write

const script = document.currentScript;
const fullUrl = script.src;

to get the currently running script with document.currentScript.

Then we get the path of the script with script.src.

Conclusion

To get the file path of the currently executing JavaScript code, we can use the document.currentScript property.