I have a method that takes obj as a parameter. I cannot change the signature, and I want to reuse it. I have to send the element as an object to this parameter, so I'm not sure how to do this. 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" ... }
var myElement = document.getElementById('myElement');
looks like a waiting dom node, so you can do it in the old fashioned simple js way.
or if you want to do it using jquery,
var myElement = $('#myElement').get(0);
You need to change:
var id = obj[0].id;
function someUsefulFunction(obj) { var id = obj.id; console.log(id); } var myElement = $('#myElement').get(0); someUsefulFunction(myElement);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="myElement"></div>
Alternative you can:
function someUsefulFunction(obj) { var id = obj[0].id; console.log(id); } var myElement = $('#myElement'); someUsefulFunction(myElement);
Source: https://habr.com/ru/post/1206670/More articles:Alternative getSystemLoadAverage () for Windows? - javaUIPopoverPresentationController / UIPopoverController loses backgroundColor when rotating the interface - iosHow to call completeHandler to execute a FetchWithCompletionHandler in Swift - swiftdifference between cin.get () and cin.getline () - c ++using nlog and writing to file as json - jsonhttps://translate.googleusercontent.com/translate_c?depth=1&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1206671/multi-device-hybrid-app-fails-to-build-the-certificate-specified-has-expired&usg=ALkJrhjSF-LUnZnRfJTnDYTw9YS2JoNuzggetComputedStyle () and cssText in IE and Firefox - javascriptHow to wait for the timer to end and then return from the function? - c #Regular expressions slowing down a program - c #How execve calls the dynamic linker / loader (ld-linux.so.2) - linuxAll Articles