JS Snippets (edit)
https://css-tricks.com/snippets/javascript/
Supported languages (file extensions)
- JavaScript (.js)
- TypeScript (.ts)
- JavaScript React (.jsx)
- TypeScript React (.tsx)
- Html (.html)
- Vue (.vue)
Remove Accents (chuyển tiếng Việt có dấu thành không dấu)
function nonAccentVietnamese(str) {
str = str.toLowerCase();
// We can also use this instead of from line 11 to line 17
// str = str.replace(/\u00E0|\u00E1|\u1EA1|\u1EA3|\u00E3|\u00E2|\u1EA7|\u1EA5|\u1EAD|\u1EA9|\u1EAB|\u0103|\u1EB1|\u1EAF|\u1EB7|\u1EB3|\u1EB5/g, "a");
// str = str.replace(/\u00E8|\u00E9|\u1EB9|\u1EBB|\u1EBD|\u00EA|\u1EC1|\u1EBF|\u1EC7|\u1EC3|\u1EC5/g, "e");
// str = str.replace(/\u00EC|\u00ED|\u1ECB|\u1EC9|\u0129/g, "i");
// str = str.replace(/\u00F2|\u00F3|\u1ECD|\u1ECF|\u00F5|\u00F4|\u1ED3|\u1ED1|\u1ED9|\u1ED5|\u1ED7|\u01A1|\u1EDD|\u1EDB|\u1EE3|\u1EDF|\u1EE1/g, "o");
// str = str.replace(/\u00F9|\u00FA|\u1EE5|\u1EE7|\u0169|\u01B0|\u1EEB|\u1EE9|\u1EF1|\u1EED|\u1EEF/g, "u");
// str = str.replace(/\u1EF3|\u00FD|\u1EF5|\u1EF7|\u1EF9/g, "y");
// str = str.replace(/\u0111/g, "d");
str = str.replace(/à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ/g, "a");
str = str.replace(/è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ/g, "e");
str = str.replace(/ì|í|ị|ỉ|ĩ/g, "i");
str = str.replace(/ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ/g, "o");
str = str.replace(/ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ/g, "u");
str = str.replace(/ỳ|ý|ỵ|ỷ|ỹ/g, "y");
str = str.replace(/đ/g, "d");
// Some system encode vietnamese combining accent as individual utf-8 characters
str = str.replace(/\u0300|\u0301|\u0303|\u0309|\u0323/g, ""); // Huyền sắc hỏi ngã nặng
str = str.replace(/\u02C6|\u0306|\u031B/g, ""); // Â, Ê, Ă, Ơ, Ư
return str;
}
var s = nonAccentVietnamese('Nguyễn Viết Mạnh');
alert(s);
Select option
var myOpts=document.getElementById('identityMenu').options;for(var i=0;i<myOpts.length;i++){console.log(myOpts[i].innerText);}
Input: Radio
Foo: <input type="radio" name="groupFoo" value="foo" checked> <br />
Bar: <input type="radio" name="groupFoo" value="bar"> <br />
Baz: <input type="radio" name="groupFoo" value="baz"> <br />
function getCheckedValue(groupName) {
var radios = document.getElementsByName(groupName);
for (i = 0; i < radios.length; i++) {
console.log(radios[i].outerHTML);
console.log(radios[i].value);
if (radios[i].checked) {
return radios[i].value;
}
}
}
window.onload = getCheckedValue('groupFoo');
Input: Checkbox
<input type="checkbox" name="device" value="Mobile">Mobile<br>
<input type="checkbox" name="device" value="Tab">Tab<br>
<input type="checkbox" name="device" value="Laptop">Laptop<br>
<input type="checkbox" name="device" value="IPod">IPod<br>
<input type="button" id="btnPrint" value="Print">
window.addEventListener('DOMContentLoaded', (event) => {
function printChecked() {
var items = document.getElementsByName('device');
var selectedItems = "";
for (var i = 0; i < items.length; i++) {
if (items[i].type == 'checkbox' && items[i].checked == true)
selectedItems += items[i].value + "\n";
}
alert(selectedItems);
}
document.getElementById("btnPrint").onclick = printChecked;
});
String Replace All
https://gist.github.com/jarvisluong/f01e108e963092336f04c4b7dd6f7e45 (JavaScript)
https://stackoverflow.com/questions/1144783/how-to-replace-all-occurrences-of-a-string
<html><body>
<script type="text/javascript">
var str1 = "a,d,k";
var str2 = str1.replace(/\,/g,"");
alert (str1);
alert (str2);
</script>
</body></html>