Nicsoft
Nicsoft

Reputation: 3712

Prepend jquery validate error message

I'm having a form which I put in a single-columned/multi-rowed table. I want the error message to be presented before the title of the input field. This was my attempt but it didn't work (if I fill in only the first input field, all other becomes invisible upon submit):

           errorPlacement: function(error, element) {
                   error.prepend(element.parent());
           },

How can I achieve this instead?

Upvotes: 0

Views: 1212

Answers (2)

Bassem
Bassem

Reputation: 3175

You probably have a syntax error

// Correct syntax
$.prepend( content, [content] )


errorPlacement: function(error, element) {
     element.parent().prepend(error);
},

// Or you can use $.prependTo( target )

$(error).prependTo(element.parent();

Upvotes: 1

Surreal Dreams
Surreal Dreams

Reputation: 26380

I'd create an empty container, like this:

<div id="errorMsg"></div>
<table id="form">
    <!-- your form table goes here -->

If you have an error, insert it into #errorMsg. You can style the by the id so it stands out and is a clear message, and you won't break the document in the process.

Later you can move the #errorMsg div elsewhere on the page with minimal effort - moving HTML is a lot easier than altering the JS to target a new spot in the DOM.

Upvotes: 1

Related Questions