Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery performing a selector on an element variable

I'm having difficulty with performing a selector operation on an element variable. First I'm selecting my table element in my page using jquery.

var $popup = null;
$popup = $("#popup_List");

<div id="popup" class="popup_block">
    <table id="popup_List"><tr><td>Name</td></tr></table>
</div>

I'm trying to perform a selector operation on the $popup variable. The following does not work

$popup("tr:last").after("<tr><td>Name</td></tr");

I'd like to use the variable approach because $("#popup_List") would have to be referenced numerous times in the code otherwise.

like image 677
amoudd Avatar asked Sep 23 '11 13:09

amoudd


People also ask

Can you use a variable in a jQuery selector?

Projects In JavaScript & JQueryYes, it is possible to pass a variable into a jQuery attribute-contains selector. The [attribute*=value] selector is used to select each element with a specific attribute and a value containing a string.

How do I select an element in jQuery by using a variable for the ID?

The #id selector selects the element with the specific id. The id refers to the id attribute of an HTML element. Note: The id attribute must be unique within a document. Note: Do not start an id attribute with a number.

How does jQuery select element?

jQuery selectors are used to "find" (or select) HTML elements based on their name, id, classes, types, attributes, values of attributes and much more. It's based on the existing CSS Selectors, and in addition, it has some own custom selectors. All selectors in jQuery start with the dollar sign and parentheses: $().


2 Answers

$popup.find("tr:last").after("<tr><td>Name</td></tr");

like image 128
Jack Avatar answered Sep 20 '22 17:09

Jack


var last = $("tr:last", popup);

just pass in the context for the jquery() method.

like image 30
Tin Nguyen Avatar answered Sep 18 '22 17:09

Tin Nguyen