Declare DOM items with $

0

Is the declaration with $ correct to assign items from the DOM to work with them later?

If not, should you assign them to normal variables with let ?

$table = $('#tblActividades');
$nuevo = $("#btnNuevo");
$buscar = $("#txtBuscar");
$ok = $("#ok");
$araba = $("#cmbAraba");
$bizkaia = $("#cmbBizkaia");
$gipuzkoa = $("#cmbGipuzkoa");

$nuevo.click(function () {
    AbrirModal(this);            
});
$ok.click(function () {
    RefrescarTabla();
}); 
    
asked by GDP 07.06.2018 в 11:01
source

1 answer

3

It is by convention the fact of using $ to refer to DOM objects accessed by jQuery , by aesthetics (to differentiate which variables will be used from the DOM in jQuery), but it is not necessary.
But you should always, as a good practice, define the scope of the variable ( var = global scope | let = local scope), you can always use var , but in some cases you may want to use var also in local area.

var $table = $('#tblActividades');
var $nuevo = $("#btnNuevo");
var $buscar = $("#txtBuscar");
var $ok = $("#ok");
var $araba = $("#cmbAraba");
var $bizkaia = $("#cmbBizkaia");
var $gipuzkoa = $("#cmbGipuzkoa");

$nuevo.click(function () {
    AbrirModal(this);            
});
$ok.click(function () {
    RefrescarTabla();
}); 

Or

let $table = $('#tblActividades');
let $nuevo = $("#btnNuevo");
let $buscar = $("#txtBuscar");
let $ok = $("#ok");
let $araba = $("#cmbAraba");
let $bizkaia = $("#cmbBizkaia");
let $gipuzkoa = $("#cmbGipuzkoa");

$nuevo.click(function () {
    AbrirModal(this);            
});
$ok.click(function () {
    RefrescarTabla();
}); 

But you must always define the variables, you can have many errors if you do not ..

    
answered by 07.06.2018 / 11:09
source