-
-
Save mikkabond/31788a367e27511f99ec1509c238814b to your computer and use it in GitHub Desktop.
javascript snippets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# вызвать событие focus на элементе с заданным идентификатором id | |
function focusOnElementWithId('id') { | |
var event = new Event('focus'); | |
document.getElementById(id).dispatchEvent(event); | |
} | |
# елемент удаляет самого себя | |
var element = document.getElementById('foo') | |
element.parentNode.removeChild(element); | |
# element удаляет самого себя внутри обработчика события onclick | |
element.onclick = function(e) { | |
this.parentNode.removeChild(this); | |
}; | |
# удалить набор элементов с классом class | |
var elements = document.getElementsByClassName("class"); | |
while (elements.length > 0) { | |
this.parentNode.removeChild(elements.item(0)); | |
} | |
# простая валидация телефона на jquery | |
$( "input[name*='phone']").on("keyup change", function(event){ | |
const regPatternChunks = ['[+]','7','[0-9]','[0-9]','[0-9]','[0-9]','[0-9]','[0-9]','[0-9]','[0-9]','[0-9]','[0-9]']; | |
if ($(this).val()) { | |
const pattern = '^' + regPatternChunks.slice(0, $(this).val().length).join('') + '$'; | |
const re = new RegExp(pattern, 'g'); | |
if (/^[+]7[0-9]{10}$/g.test($(this).val())) { | |
$(this).removeClass('invalid'); | |
} else { | |
$(this).addClass('invalid'); | |
} | |
if (!re.test($(this).val())){ | |
$(this).val($(this).val().match(/^([+]7)|([0-9])/g).join('').substring(0, regPatternChunks.length)); | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment