I have a series of labels
in my view JSP
. Each label
shows a unit of measure and in case you want to edit it, it is replaced via javascript by a input
via button.
NOTE: both the labels and the input are elements with class="CCC_UNIDAD" to iterate them later. The html
simplified result would look something like this:
<label class="CCC_UNIDAD">%</label>
<input class="CCC_UNIDAD">
<input class="CCC_UNIDAD" value="$">
<label class="CCC_UNIDAD">€</label>
$.each($(".CCC_UNIDAD"), function (index, value) {
var valor = value.textContent === undefined
? (value.value === undefined ? "" : value.value)
: value.textContent;
alert("VALOR = " + valor);
});
As you can see, in the snippet, I try to get the value of the label ( value.textContent
), if it does not have it, I look for the value of the input value.value
but it is not working correctly.
How can I differentiate what type of element the variable is value
in order to get the attribute corresponding to each type?
First of all as @rnrneverdies mentions the HTML is not well formed. The input tags do not have a closing tag but are self-closing.
The correct form should be:
Then to get the value or the text you can use the operator
||
Finally in your case everything would be as the next snippet
You can use the property
nodeName
, here is an example.(you will need to open the F12 console to see the results)
note: the input element, in html5, does not have a closing tag. It is initialized as seen below.
If you want to know the object's tag you can do this (with your code):
The .tagName tells you if it's 'DIV', 'INPUT', 'TABLE', etc...