0

I am trying to remove a dynamically created element using jQuery.

Here is my example: http://jsfiddle.net/4uxbu/

When you click submit it highlights the required fields and displays the text This field is required. after the input. When the user clicks on the input the red box goes away, I want the text below the input to go away as well.

The message is set using the following code:

$('#' + required[i]).after('<p style="font-weight:bold;" id="' + required[i] + '_error">' + emptyerror + '</p>');

and I've tried to remove it with:

$('p#' + required[i] + '_error').remove();

But I can't get it to work properly.

Required is as below

required = ["vidHeight", "title", "vidWidth", "vidLen", "thumb", "file"];

Please see the fiddle for the full code.

Thanks

3
  • This is pretty round-about. Why not just add a ".error" class to your error messages, and then do a single $('form p.error').remove()? Simple, clean, elegant. Commented Aug 20, 2012 at 14:18
  • Would that not remove all of them? Thats why I put a unique class for each error Commented Aug 20, 2012 at 14:21
  • It seems like that's exactly what you're trying to do: Remove and re-add them each time. Commented Aug 20, 2012 at 15:03

3 Answers 3

3
$('p#' + required[i] + '_error').remove();

Well, required[i] doesn't make any sense because you aren't in a loop where i is defined anymore. Try this:

$('#' + $(this).attr("id") + '_error').remove();

See: http://jsfiddle.net/4uxbu/1/

EDIT

Updated to handle the case of showing multiple errors when submitting multiple times with errors: http://jsfiddle.net/4uxbu/2/

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

6 Comments

This works perfectly! Except for the file upload fields, any ideas on them?
Ah, I just realized why I was getting the error because of the frames. My mistake.
If you submit multiple times, the error message keeps popping up a bunch. I modified the fiddle to handle that case: jsfiddle.net/4uxbu/2
When I use the code on my server I get Uncaught SyntaxError: Unexpected token ILLEGAL, how can I solve that?
Something else is going on...which line does it say is causing the problem?
|
0

The [i] will have no affect while not in the for loop, so I would suggest changing the following:

$(this).removeClass("needsfilled");
$('#' + required[i] + '_error').remove();

To something more like:

$(this).removeClass("needsfilled").next('#'+$(this).attr('id')+'_error').remove();

Comments

0

I would not store the names, but rather an array of the error messages. If you kept an array of actual items being added then you would reduce the number of DOM queries and have an easy way to address them. You could even create an object that associates the controls with the messages.

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.