Suppose, a HTML content like:
<div class="form-group">
<label class="control-label">Title <span class="text-danger">*</span></label>
<input type="text" name="sms_title" id="sms_title" class="form-control" required="required" placeholder="SMS Title" onblur="_field_validate('sms_title')" >
</div>
How can I get label content title in JavaScripts. My JS Function:
function _field_validate(fieldId) {
var chars = /[^a-zA-Z 0-9_]/g;
var elem = document.getElementById(fieldId);
if (typeof elem !== 'undefined' && elem !== null) {
var input = document.getElementById(fieldId).value;
if (!input) {
return false;
} else if (chars.test(input)) {
console.log("this not proper stirng. You may do something here.");
return false;
} else {
var url = site_url + "/notice/get_sms";
get_data(url).then(function (result) {
//PROMISE return data
if (!result) {
return false;
} else {
var i = 0;
var flag = 0;
result.forEach(function () {
if (result[i][fieldId] == input) {
console.log(result[i][fieldId]);
flag = 1;
var parent = document.getElementById(fieldId).parentNode;
var label = parent.getElementsByTagName("label").innerHTML;
console.log(label); //here console the label content
alert.innerHTML = 'alert-text';
alert.style.margin = 0 + "px";
alert.style.paddingLeft = 6 + "px";
alert.className = 'text-danger';
alert.id = 'alert-field';
parent.appendChild(alert);
}
i++;
});
}
});
}
}
}
get_data function return a database data. this is evrything is ok. but how i get the label content?