Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JQuery remove previous element only if a button 1

Status
Not open for further replies.

tshad

Programmer
Jul 15, 2004
386
US
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">&times;</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

 
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top