0

I'm stuck why the .not() selector does not affect on validating. This is my HTML form:

<input name="Title" id="Title" type="text" />
<div id="ErrorTitle" class="hiddenbox"></div>

<input name="Text" id="Text" type="text" />
<div id="ErrorText" class="hiddenbox"></div>

<input name="Price" id="Price" type="text" />
<div id="ErrorPrice" class="hiddenbox"></div>

And this the jquery:

if($('#Title').val() == '')
{
    $('#Title').focus();
    ErrorBox('Please enter title', 'ErrorTitle'); // Write message in .hiddenbox
    $('.hiddenbox').not('#ErrorTitle .hiddenbox').hide();
}
else if($('#Text').val() == '')
{
    $('#Text').focus();
    ErrorBox('Please enter text', 'ErrorText'); // Write message in .hiddenbox
    $('.hiddenbox').not('#ErrorText .hiddenbox').hide();
}
else if($('#Price').val() == '')
{
    $('#Price').focus();
    ErrorBox('Please enter price', 'ErrorPrice'); // Write message in .hiddenbox
    $('.hiddenbox').not('#ErrorPrice .hiddenbox').hide();
}

When submitting the form no one error message would display! Do I wrong it?!

2
  • 1
    As you are using id selector simply use not('#ErrorTitle') there is no need to class selector Commented Jan 20, 2015 at 15:40
  • 1
    Learn how to use the very basic CSS selectors before starting to code. Space denotes hierarchy. Commented Jan 20, 2015 at 15:42

1 Answer 1

1

I think you are wrong in your selector:

$(".validate").on('click', valid);
        function valid() {

            if($('#Title').val() == '')
            {

                $('#Title').focus();
                $("#ErrorTitle.hiddenbox").text("errortitle"); // Write message in .hiddenbox
                $("#ErrorTitle.hiddenbox").show();
                $('.hiddenbox').not('#ErrorTitle.hiddenbox').hide();
            }
            else if($('#Text').val() == '')
            {
                $('#Text').focus();
                $("#ErrorText.hiddenbox").text("errortext"); // Write message in .hiddenbox
                $("#ErrorText.hiddenbox").show();
                $('.hiddenbox').not('#ErrorText.hiddenbox').hide();
            }
            else if($('#Price').val() == '')
            {
                $('#Price').focus();
                 $("#ErrorPrice.hiddenbox").text("errorprice"); // Write message in .hiddenbox
                $("#ErrorPrice.hiddenbox").show();
                $('.hiddenbox').not('#ErrorPrice.hiddenbox').hide();
            }
        }

Should do the trick. Here is the fiddle: http://jsfiddle.net/BenoitNgo/hm128k63/

Sign up to request clarification or add additional context in comments.

1 Comment

That saves me:) Thank you!

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.