Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easier way to get a jQuery object from appended element

Is there an easier/quicker way to get the element added using jQuery append:

How to get the $selectors element:

$container.append('<div class="selectors"></div>'); var $selectors = $('.selectors', $container); 

I tried:

var $selectors = $container.append('<div class="selectors"></div>'); 

but that makes $selectors = $container

Maybe that's the quickest/best way. Just checking.

like image 438
slolife Avatar asked Sep 18 '09 08:09

slolife


People also ask

What is $() in jQuery?

$() = window. jQuery() $()/jQuery() is a selector function that selects DOM elements. Most of the time you will need to start with $() function. It is advisable to use jQuery after DOM is loaded fully.

What is the jQuery method for retrieving the HTML content of an element?

To get HTML content of an element using jQuery, use the html() method. The html() method gets the html contents of the first matched element.

Which method of jQuery is used to get the contents of all the matched elements?

The html() method in jQuery is used to get the contents of the first element in the set of matched elements or is used to set the HTML contents of every matched element. It returns the content of the first matched element. This function does not accept any arguments.

How do you use .append in JS?

1) Using the append() method to append an element example First, select the ul element by its id by using the querySelector() method. Second, declare an array of languages. Third, for each language, create a new li element with the textContent is assigned to the language.


1 Answers

Why not just:

var el = $('<div class="selectors"></div>'); $container.append(el); 

?

Then you have access to 'el'.

like image 120
cletus Avatar answered Sep 20 '22 17:09

cletus