I want to invoke a custom method on a DOM element
like this :
<div id="MyObject">
<!-- some elements -->
</div>
<script>
function doSomething() {
// do something with input DOM element
}
$("MyObject").doSomething();
</script>
How can I develop this problem? Is it necessary to use jQuery or not?
You do not need to use jQuery. You can use
document.getElementById('MyObject')
to get a reference to the DOM node.To run your
doSomething
function on it, you would need to add a node parameter to it something like this:To have it chained, you would need to add to the
Element
interface which all DOM nodes implement (rereading, I meant inherit from). If you go that way, you could do:JSFiddle: http://jsfiddle.net/6Lyb4b9p/
MDN: getElementById