Need some clarification about jQuery objects that don't reside in the DOM

shenkwen

I was trying to use browser console to do some HTML code manipulation. In console, I input

s = $('<div><span>ddd</span></div>');
s.remove('span');

Be noted the jQuery object s is not in the DOM, it only lives in the console. It turns out the span isn't removed from s. On the other hand, if <div><span>ddd</span></div> was in HTML, the span will surely be removed.

This brings up a question that has been confusing me for a long time. If I understand right, using $(), I can turn many things into jQuery objects, even if they are not actually in the DOM. But what is the difference between this kind of jQuery objects and the those jQuery objects that are linked to some DOM elements? And in the above code, in order to actually remove the span and get an output as <div></div>, do I have to write it into DOM?

dfsq

It's because you are not using $.fn.remove properly. Correct way to remove child span is to find it first:

s = $('<div><span>ddd</span></div>');
s.find('span').remove();

When you provide a selector to remove, jQuery filters supplied collection by this selector. However, in your case there is nothing that can be filtered out, since clearly div is not span. So s.remove('span'); removes nothing.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Need some clarification about beta/alpha testing on the developer console

From Dev

need some clarification about DAG (Directed Acyclic Graph)

From Dev

Objects added dynamically to the DOM don't respond to jQuery Events

From Dev

Objects added dynamically to the DOM don't respond to jQuery Events

From Dev

Need clarification about constant expressions

From Dev

Need some clarification on #pragma once

From Dev

I need some clarification regarding this example on Stroustrup's new book about ADL

From Dev

I need some clarification about whether I should (could?) deallocate view-related UI elements or not

From Dev

PHP: need some clarification about custom error and exception handlers and trigger_error

From Dev

I need some clarification on some code samples

From Dev

Brief: Some game objects move, some don’t

From Dev

Brief: Some game objects move, some don’t

From Dev

jquery datatable some settings don't work

From Dev

Need clarification about Protobuf-net RuntimeTypeModel

From Dev

Need clarification about usage of mahout with hadoop

From Dev

Need some clarification regarding reference types

From Dev

Need some clarification on running Cassandra nodetool repairs

From Dev

I need some C# generics clarification

From Dev

Need some clarification regarding reference types

From Dev

I need some clarification on Java while loop

From Dev

entity framework clarification about model objects and their methods

From Dev

Angular - Some Attributes Need Explicit Binding to $scope, Others Don't?

From Dev

Why don't Option's Some and None variants need to be qualified?

From Dev

Why some Boost functions don't need prefixing with namespace

From Dev

Angular - Some Attributes Need Explicit Binding to $scope, Others Don't?

From Dev

Benefit to making objects static, even though they don't need to be?

From Dev

IEnumerable<T> and IEnumerator - some clarification please

From Dev

IEnumerable<T> and IEnumerator - some clarification please

From Dev

I don't understand JSON. Need some help clearing up some things

Related Related

  1. 1

    Need some clarification about beta/alpha testing on the developer console

  2. 2

    need some clarification about DAG (Directed Acyclic Graph)

  3. 3

    Objects added dynamically to the DOM don't respond to jQuery Events

  4. 4

    Objects added dynamically to the DOM don't respond to jQuery Events

  5. 5

    Need clarification about constant expressions

  6. 6

    Need some clarification on #pragma once

  7. 7

    I need some clarification regarding this example on Stroustrup's new book about ADL

  8. 8

    I need some clarification about whether I should (could?) deallocate view-related UI elements or not

  9. 9

    PHP: need some clarification about custom error and exception handlers and trigger_error

  10. 10

    I need some clarification on some code samples

  11. 11

    Brief: Some game objects move, some don’t

  12. 12

    Brief: Some game objects move, some don’t

  13. 13

    jquery datatable some settings don't work

  14. 14

    Need clarification about Protobuf-net RuntimeTypeModel

  15. 15

    Need clarification about usage of mahout with hadoop

  16. 16

    Need some clarification regarding reference types

  17. 17

    Need some clarification on running Cassandra nodetool repairs

  18. 18

    I need some C# generics clarification

  19. 19

    Need some clarification regarding reference types

  20. 20

    I need some clarification on Java while loop

  21. 21

    entity framework clarification about model objects and their methods

  22. 22

    Angular - Some Attributes Need Explicit Binding to $scope, Others Don't?

  23. 23

    Why don't Option's Some and None variants need to be qualified?

  24. 24

    Why some Boost functions don't need prefixing with namespace

  25. 25

    Angular - Some Attributes Need Explicit Binding to $scope, Others Don't?

  26. 26

    Benefit to making objects static, even though they don't need to be?

  27. 27

    IEnumerable<T> and IEnumerator - some clarification please

  28. 28

    IEnumerable<T> and IEnumerator - some clarification please

  29. 29

    I don't understand JSON. Need some help clearing up some things

HotTag

Archive