Maizere Pathak
Maizere Pathak

Reputation: 299

jquery method of selecting elements

what is the real difference between the following code:

<p>Maizere1</p>
<p>Maizere2</p>
<p>Maizere3</p>
<script>
$("p").text($(this).text()).get(0);

vs

$("<p>").text($(this).text()).get(0);//actually this line is what giving me trouble 

what does $("<p>"> this do?
i heard that $("<p>") will first actually check where the element exist or not ,if not only will create element 

Upvotes: 0

Views: 55

Answers (3)

Jan Hommes
Jan Hommes

Reputation: 5152

$("<p>") creats a new element and you can use .append() to add it to the dom while $("p") selects them.

correct is to use $("<p>") like this: $("<p />"). But jQuery allows both.

Example:

<p></p>

$("p").append($("<p>test</p>").addCLass("test"));

result:

<p class="test">
  <p>test</p>
</p>

Upvotes: 2

ncremins
ncremins

Reputation: 9200

$('<p>') isn't a valid jquery selector but it will however create a <p> element which I don't think is what you're trying to solve here.

Upvotes: 5

Jing
Jing

Reputation: 700

$("p") - select p element
$("<p>") - creating p element on the fly

Upvotes: 2

Related Questions