I have a page layout like this
<input id="name" class="mandotary" type="text">
<label for="name">Name must be entered</label>
<input id="code" class="mandotary" type="text">
<label for="code">Code must be entered</label>
Now I want to hide the label element when the page loads. If he lost focus, and the value is zero, then the label must be indicated otherwise the label must be hidden. I tried this
var inputElementArray = $("input.mandotary");
$.each(inputElementArray, function(index, element){
var id = $(element).attr("id");
var label = $("label").attr("for", id);
$(element).focus(function(){
var next = $(element).next();
$(element).next().hide();
});
$(element).blur(function(){
var text = $(element).val();
if (text == "") {
var next = $(element).next();
$(element).next().show();
}
});
});
But the line
var label = $("label").attr("for", id);
Give me both tags. I just want a label for which the attribute value is id (name or code). I get the name or code as id in var id . But he finds and leabes. How can I find a label for which the attribute value is equal to my input element identifier?
thank
source
share