INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

JQuery remove previous element only if a button

JQuery remove previous element only if a button

(OP)
I need to remove the previous element only if it is a button. And then I need to unwrap div I am looking at (but only if the previous element was a button that I just deleted - if not don't do anything).

CODE

<div>
	<div class="alert alert-danger alert-dismissible" role="alert">
		<button type="button" class="close" data-dismiss="alert">
			<span aria-hidden="true">×</span><span class="sr-only">Close</span>
		</button>
		<div class="validation-summary-errors" data-valmsg-summary="true">
			<ul>
				<li>Invalid input</li>
			</ul>
		</div>
	</div>
</div> 

So here I want to do something like:

CODE

previous = $("div[data-valmsg-summary]").prev()
if( previous.get(0).tagName == "BUTTON)
{
   $("div[data-valmsg-summary]").prev().remove()
   $("div[data-valmsg-summary]").unwrap()
} 

Is this the best way to do this?

Thanks

Tom

RE: JQuery remove previous element only if a button

in jQuery the syntax would more be

CODE

$("div[data-valmsg-summary]").each(function(i){
  if($(this).prev().is('button')){
    $(this).prev().remove();
    $(this).unwrap();
  }
}); 

fiddle here

RE: JQuery remove previous element only if a button

(OP)
That is better.

Thanks

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close