1

Im trying to check if the textfields is null using jquery, but its not working, The textfields is dynamically created and has name quiztxtBox[], with bracket, indicating it is an array, and when a certain field is null the textfield will turn red,. but the problem is it is not working, hmmm Here is my codes

$("#quiztxtBox[]").each(function()
              {
                  if($("#quiztxtBox[]").val()=="")
                  {
                      $("#quiztxtBox[]").nextAll('span').html("Field needs filling");
                      $("#quiztxtBox[]").css({"background-color":"#f6d9d4"});

                  }
          });

EDIT
This is the html codes, the span part is not working, I wonder why, this is the html for dynamically creating textboxes..

<div id="QuestionTBDiv1" >
                                        <label>Question</label><br/>
                                        <input type="text" name="quiztxtBox[]" size="57" id="quiztxtBox[]" placeholder="Question #1"><br/>
                                        <label>Answer</label><br/>
                                        <input type="text" name="quiztxtBox[]" size="24" id="answer[]" placeholder="Choice A">&nbsp;<input type="radio" class = "choiceA" name="correct_answer1" value="A">&nbsp;
                                        <input type="text" name="quiztxtBox[]" size="24" id="answer[]" placeholder="Choice B">&nbsp;<input type="radio" class = "choiceB" name="correct_answer1" value="B"><br/>
                                        <input type="text" name="quiztxtBox[]" size="24" id="answer[]" placeholder="Choice C">&nbsp;<input type="radio" class = "choiceC" name="correct_answer1" value="C">&nbsp;
                                        <input type="text" name="quiztxtBox[]" size="24" id="answer[]" placeholder="Choice D">&nbsp;<input type="radio" class = "choiceD" name="correct_answer1" value="D"><br>
                                        <span name="errMchoice" class="errorMsg"></span>
                                        </div>  
4
  • 1
    "has name quiztxtBox[]" - name or id? Commented Feb 3, 2014 at 9:00
  • 2
    attribute selector $('input[name="quiztxtBox[]"]')?? Commented Feb 3, 2014 at 9:05
  • @ArunPJohny you should consider answering this way as of OP comments to the answer this works perfect. Commented Feb 3, 2014 at 9:06
  • Note that no matter what selector you use, .val() will only ever return the value of the first element in the selector's elements. Commented Feb 3, 2014 at 9:11

2 Answers 2

1

Try with:

$("[name='quiztxtBox[]']").each(function(){
  if ($(this).is(':empty')) {
    $(this).nextAll('span').html("Field needs filling");
    $(this).css({"background-color":"#f6d9d4"});
  }
});
Sign up to request clarification or add additional context in comments.

3 Comments

Hi, this works except for this part $(this).nextAll('span').html("Field needs filling");
@user2901740 I don't see your HTML, so I cannot help you/
well, it is consist ko 4 textbox and 1 span tag, 4 textbox and 1 tag so on .. How I wish I can post it here.. hmm
0

Try this:

change id to class that is, change id="#quiztxtBox[]" to class=".quiztxtBox" then,

$(".quiztxtBox").each(function()
{
    if($(this).val()=="")
    {
         $(this).nextAll('span').html("Field needs filling");
         $(this).css({"background-color":"#f6d9d4"});
    }
});

i think you need this:

check demo @ http://jsfiddle.net/renishar/S73gq/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.