Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get as object using id in jquery

Tags:

jquery

I have a method which take obj as parameter. I can't change the signature and I wan to reuse it. I have to send an element as object to this parameter so not sure how to do it. Can anyone show me?

function someUsefulFunction(obj) {
  var id = obj.id;
  //do other stuff
}

{
  ...
  var myElement = $('#myElement');
   someUsefulFunction(myElement); //getting error "TypeError: obj.id is undefined"
  ...
}
like image 593
αƞjiβ Avatar asked Nov 11 '14 20:11

αƞjiβ


People also ask

Which jQuery statement selects the dom element with an id of testid'?

Calling jQuery() (or $() ) with an id selector as its argument will return a jQuery object containing a collection of either zero or one DOM element. Each id value must be used only once within a document.

How do I know which ID is clicked in jQuery?

click(function() { var id = $(this). attr('id'); $container. cycle(id. replace('#', '')); return false; });

How do you check if the ID exists in jQuery?

In jQuery, you can use the . length property to check if an element exists. if the element exists, the length property will return the total number of the matched elements. To check if an element which has an id of “div1” exists.

How do you target a class in jQuery?

In jQuery, the class and ID selectors are the same as in CSS. If you want to select elements with a certain class, use a dot ( . ) and the class name. If you want to select elements with a certain ID, use the hash symbol ( # ) and the ID name.

How to get the ID of an element in jQuery?

When we need to find a single and unique element at that time we can use jQuery get by element id. JQuery uses attr () method to find out the id of an element. The get by element id is a faster method because it directly calls the JavaScript engine.

How to get objects by id class tag and attribute in jQuery?

How to get objects by ID, Class, Tag, and Attribute in jQuery? Here’s how you can get objects by ID Selector (#id), by Class Selector (.class), by Tag, and Attribute (.attr ()). The element class selector selects all the elements which match with the given class of the elements.

How to get objects by id selector in HTML?

Here’s how you can get objects by ID Selector (#id), by Class Selector (.class), by Tag, and Attribute (.attr ()). The element class selector selects all the elements which match with the given class of the elements.

How to use jQuery IDs to manipulate Dom objects?

With the Id, you can now use jQuery to reference and manipulate that specific DOM object. The following example changes the button’s text when the page loads in the browser: The hash indicates to jQuery that you’re using an Id identifier. You can use other methods to select a part of the DOM such as CSS class or parent and child elements.


1 Answers

var myElement = document.getElementById('myElement');

looks like its expecting a dom node, so you can do this the old fashioned plain js way.

or if you want to do it with jquery,

var myElement = $('#myElement').get(0);
like image 50
Rooster Avatar answered Nov 15 '22 06:11

Rooster