How to check if there is a specific text box under a specific div

I have this code:

<PlaceHolder> <div class="greenDiv"> <asp:TextBox ID="a" runat="server" /> </div> <div class="greenDiv"> <asp:TextBox ID="ab" runat="server" /> </div> </PlaceHolder> 

I need to tell the user if he left the text box empty, I tried something like this .. (does not work) What did I miss?

  $('.greenDiv > input:text').blur(function () { if (!$(this).val()) { alert("fill this field"); } }); 
+5
source share
5 answers

Try input[type=text] instead

 $('.greenDiv>input[type=text]').blur(function () { if (!$.trim($(this).val())) { alert("fill this field"); } }); 
+4
source
 $('.greenDiv > input[type=text]').on('blur', function (e) { if (!$(e.currentTarget).val()) alert('fill this field'); }); 
+3
source

Instead of input:text you should use input[type=text]

 $('.greenDiv>input[type=text]').on("blur",function () { if (!$(this).val()) { alert("fill this field"); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="greenDiv"> <input type="text"> </div> <div class="greenDiv"> <input type="text"> </div> 
+1
source

try it

  <PlaceHolder> <div class="greenDiv"> <asp:TextBox ID="a" class="x" runat="server" /> </div> <div class="greenDiv"> <asp:TextBox ID="ab" class="x" runat="server" /> </div> </PlaceHolder> 

below - jQuery

  $('.greenDiv .x').blur(function () { if (!$(this).val()) { alert("fill this field"); } }); 
+1
source

Try the following:

 $('.greenDiv > input:text').blur(function () { if ($.trim($(this).val()) === '') { alert("fill this field"); } }); 

Your selector is fine. When you use !$(this).val() , you allow inputs with spaces (or any other invisible character) to fail. Entering with one space is not technically empty. Instead, try $.trim(something) === '' .

+1
source

Source: https://habr.com/ru/post/1209241/


All Articles