4

I'm trying to write a small snippet of jQuery that validates my input fields. If they don't validate I want to write an error message to an empty <p> tag, only I can't seem to target it correctly. Can anybody see/explain where im going wrong?

http://jsfiddle.net/xG2KS/19/

$(".nextform").click(function() {
    var empty = $(this).parent().find("li input").filter(function() {
        return this.value === "";
    });
    if(empty.length) {
        $(this).find('p.feedback').html('error');
    }
});
<div>
    <li><input type="text" /><br /></li>
    <li><input type="text" /><br /></li>
    <li><input type="text" /><br /></li>
    <li><p class="feedback"></p>
    <input type="submit" value="next" class="next" /></li>
</div>
1
  • 1
    Your code is not semantically correct, you cannot nest <li> tags within a <div> tag Commented Jan 18, 2012 at 10:20

5 Answers 5

2
$(".next").click(function() {
    var empty = $(this).parent().parent().find("li input").filter(function() {
        return this.value === "";
    });
    if(empty.length) {
        $(this).prev('p.feedback').html('error');
    }
});

I do not find .nextform but just .next, and should use $(this).parent().parent() because the click target is in li

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

2 Comments

While this is totally valid answer, I would recommend adding some kind of .container class to parent div. This way your code will not break in case you choose to wrap your submit button in some other tag.
You are right, my code base on the structure, using class might be better
0
  • In your sample HTML, there is no element with the class .nextform. Try selecting on "input.next" or, if you want to be more specific, "input[type=submit].next".
  • <li>'s should always be inside an <ul> or <ol>.
  • <input>'s should only have type="submit" if they are inside a form and used to submit that form. If they are not, give them type="button".
  • Use .closest to find the nearest ancestor of a certain type, e.g.: $(this).closest("div").
  • It's good practice to be as explicit as possible, so use: if (empty.length > 0) { ... }

http://jsfiddle.net/PPvG/xG2KS/38/

$("input.next").click(function() {
    var parent = $(this).closest('div');
    var empty = parent.find("input[type=text]").filter(function() {
        return this.value === "";
    });
    var feedback = parent.find('p.feedback');
    if (empty.length > 0) {
        feedback.text('error');
    } else {
        feedback.text('');        
    }
});
<div>
    <ul>
        <li><input type="text" /><br /></li>
        <li><input type="text" /><br /></li>
        <li><input type="text" /><br /></li>
        <li>
            <p class="feedback"></p>
            <input type="button" value="next" class="next" />
        </li>
    </ul>
</div>

Comments

0

You should use prev()

$(".nextform").click(function() {
    var empty = $(this).parent().find("li input").filter(function() {
        return this.value === "";
    });
    if(empty.length) {
        $(this).prev('p.feedback').html('error');
    }
});

because find() looks for the descendants

Get the descendants of each element in the current set of matched elements, filtered by a selector, jQuery object, or element.

Comments

0

New Fiddle: http://jsfiddle.net/kmendes/xG2KS/32/

This is how the code should look like:

$(".next").click(function() {
    var found = $(this).parents("div").find("li input").filter(function() {
        return this.value === "";
    });
    if(found.length) {
        $(this).parent().find('p.feedback').html('error');
    }
});

Comments

0
$(".next").click(function() {
    var empty = $(this).parent().find("li input").filter(function() {
        return this.value === "";
    });
    if(empty.length) {
        $(this).parent().find('p.feedback').html('error');
    }
});

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.