Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i remove an outer div with jquery

Tags:

jquery

Ok so i have this structure

 <div class="field_with_errors">
    <input id="count" name="count" size="2" type="text" />
    <label class="message" for="count_for">Required</label>
 </div>

How do i remove the outer field_with_errors and the inner message and just leave the input tag

if I do

$("#count").closest(".field_with_errors").remove()

it removes the entire div

I can remove the inner .message first but not sure how to remove the outer

$("#count").closest(".field_with_errors").find('.message').remove()

Any ideas

like image 276
Matt Elhotiby Avatar asked May 20 '12 13:05

Matt Elhotiby


People also ask

What is unwrap in jQuery?

The unwrap() method is an inbuilt method in jQuery which is used to remove the parent element from the selected element. Syntax: $(selector).unwrap()

How do I remove all elements from a div?

The empty() method removes all child nodes and content from the selected elements.

How do I empty a div?

To clear the contents of a div element, set the element's textContent property to an empty string, e.g. div. textContent = '' . Setting textContent on the element removes all of its children and replaces them with a single text node of the provided value.

What is difference between remove and hide in jQuery?

hide() sets the matched elements' CSS display property to none . remove() removes the matched elements from the DOM completely. detach() is like remove() , but keeps the stored data and events associated with the matched elements.


1 Answers

use replacewith() method,

$(".field_with_errors").replaceWith($("#count"));​

here is the fiddle example

like image 63
adeneo Avatar answered Sep 20 '22 00:09

adeneo