Skip to content

Instantly share code, notes, and snippets.

View UltCombo's full-sized avatar

Fabricio Matte UltCombo

View GitHub Profile
console.log( $('#precisaExistir').length );
var $el = $('#precisaExistir');
if ($el.length) {
$el.css('backgroundColor', 'green');
} else {
alert('Deu pau aqui tio.');
}
var $el = $('#precisaExistir');
if ($el.length) {
$el.css('backgroundColor', 'green');
} else {
throw '#precisaExistir não encontrado.';
}
var $el = $('#precisaExistir');
//realizamos testes primeiro
if (!$el.length) {
throw '#precisaExistir não encontrado.';
}
//passou pelos testes básicos, executamos a funcionalidade da função/script
$el.css('backgroundColor', 'green');
//para testar se nosso objeto jQuery possui elementos:
if ($el.length) {/*...*/}
//isto é equivalente à:
if ($el.length !== 0) {/*...*/}
//Já que .length sempre é um número >= 0, também podemos escrever:
if ($el.length > 0) {/*...*/}
//e para ver se não possui nenhum elemento:
if (!$el.length) {/*...*/}
//isto equivale à:
$('html').appendTo('body');
$('#foo').css('border-radius', 10);
$('#foo').css('borderRadius', 10);
document.getElementById('foo').style.borderRadius = '10px';
@UltCombo
UltCombo / gist:6445413
Last active December 22, 2015 08:29
syntax error due to invalid character in identifier name
element.style.border-radius = //[...]
var o = { 'foo-bar!@ #': 1 };
console.log( o['foo-bar!@ #'] ); //1