Respuestas:
Sí, puede usar la .idpropiedad del elemento dom , por ejemplo:
myDOMElement.id
O algo como esto:
var inputs = document.getElementsByTagName("input");
for (var i = 0; i < inputs.length; i++) {
alert(inputs[i].id);
}
myDOMElement.idtambién puede devolver un elemento hijo con la identificación o el nombre de 'identificación'. Como se ve aquí en este jsfiddle
Sí, simplemente puedes decir:
function getID(oObject)
{
var id = oObject.id;
alert("This object's ID attribute is set to \"" + id + "\".");
}
Mira esto: Atributo ID | Propiedad id
Esto también funcionaría:
document.getElementsByTagName('p')[0].id
(Si el elemento es el primer párrafo de su documento)
getElementsByClassNameno es compatible con IE (antes de IE9).
Super Easy Way es
$('.CheckBxMSG').each(function () {
var ChkBxMsgId;
ChkBxMsgId = $(this).attr('id');
alert(ChkBxMsgId);
});
Dime si esto ayuda
En el controlador de eventos, puede obtener la identificación de la siguiente manera
function show(btn) {
console.log('Button id:',btn.id);
}
<button id="myButtonId" onclick="show(this)">Click me</button>
Debe verificar si es una cadena para evitar obtener un elemento hijo
var getIdFromDomObj = function(domObj){
var id = domObj.id;
return typeof id === 'string' ? id : false;
};
Si. Puede obtener un elemento por su ID llamando document.getElementById. Devolverá un nodo de elemento si se encuentra, y de lo nullcontrario:
var x = document.getElementById("elementid"); // Get the element with id="elementid"
x.style.color = "green"; // Change the color of the element