4

I want to remove input fields when certain events happen using jQuery:

$('#myInputId').remove();

But if I try it, the surrounding div, which is automatically added by jQueryMobile, is not being removed. So the user still sees a div, which looks like an input field, but which is obviously not usable because the inner input is not there any more.

Does jQueryMobile offer something which is guaranteed not to break when the next upgrade shows up?

6
  • try $('#myInputId').parent().remove(); Commented Apr 10, 2013 at 12:26
  • Which input filed? In jQuery Mobile, input type="text" don't have parent div container. Commented Apr 10, 2013 at 12:30
  • I use the new Version 1.3 and the input with type="text" have a surrounding div with class "ui-input-text" (and several other classes). Commented Apr 10, 2013 at 12:38
  • Damn I was testing it on a version 1.2 Commented Apr 10, 2013 at 12:41
  • Do you need to remove it or disable it? Commented Apr 10, 2013 at 12:53

3 Answers 3

5

To remove Text-input field using .closest(); since the parent DIV is added by jQuery Mobile and cant have an ID.

Demo


$('.selector').closest('div').remove();

To remove its' label

$('.selector').closest('div').prev('label').remove();

jQuery Mobile renders Text-input this way. It wraps it with a div and placed the label before it with no enhancement. It enhances the input by adding <a> and <span> to style textbox.

<label>Text Input</label>

<div>
 <input type="text" />
  <a href="#">
   <span>
    <span>&nbsp;
    </span>
   </span>
  </a>
</div>
Sign up to request clarification or add additional context in comments.

Comments

1

try removing its parent, if the div is the direct parent..

$('#myInputId').parent().remove()

http://jsfiddle.net/F45Mg/

3 Comments

I am a little bit afraid that the code breaks as soon as jQueryMobile decides to change in the behaviour in the feature or for certain browsers. But thanks.
Why? If this is the case, you haven't given enough to go off of. I said "if the div is the direct parent", if this is not the case... you can make it more specific, go to a certain ancestor based on a certain ID or whatever would fit your case.
The div is automatically added by jQueryMobile. So I cannot give it any specific ID. I can obviously look at the classes it has, but I could imagine jQueryMobile changing them in the future. So I would love to use a method that jQueryMobile offers which is guaranteed to not break in the future.
0
 $('#myInputId').parent().remove();

1 Comment

the reason that is working is because you just modified my jsfiddle (in which i manually added a sample div) and checked the jquery mobile box (which automatically adds a div), making two div wrappers.

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.