Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get all related javascript to an html element

Is there a way to get all javascript associated with an html element by class name returned in an array? Any suggestion as to how one would achieve doing this? Are there any node packages that would allow me to do something like this?

For example:

HTML

<div class="click_me">Click Me</div>

JS

$('.click_me').on('click', function() { alert ('hi') });

I would want something like (psuedo-code either on the client or server side):

function meta() {
   let js = [];
   js = getAllJavascriptByClassName('click_me');
   console.log(js[0]);
}

Output of meta()

$('.click_me').on('click', function() { alert ('hi') });
like image 592
Ron I Avatar asked May 31 '16 16:05

Ron I


People also ask

How will you get all the matching tags in a HTML file?

If you want to find all HTML elements that match a specified CSS selector (id, class names, types, attributes, values of attributes, etc), use the querySelectorAll() method.

How can we fetch all attributes for an HTML element in JavaScript?

To get all of the attributes of a DOM element: Use the getAttributeNames() method to get an array of the element's attribute names. Use the reduce() method to iterate over the array. On each iteration, add a new key/value pair containing the name and value of the attribute.

How can I get all of the elements with the same class name in JavaScript?

Get elements by class namegetElementsByClassName('class-name') , it grabs all the elements that have the same class name and returns us an HTML Collection that we can index or iterate to get the elements that we need in particular.

How do you get an array of all the DOM elements with a certain selector?

To get all DOM elements by an attribute, use the querySelectorAll method, e.g. document. querySelectorAll('[class="box"]') . The querySelectorAll method returns a NodeList containing the elements that match the specified selector.

How do I get all elements of tag Div inside HTML?

Get IDs of all DIV elements In this case, use the getElementByTagName() method. The getElementByTagName() takes a parameter in the form of the tagname. Here, the tagname would be DIV. You can use the above methods to get the ID any element on a web page.


2 Answers

This will pull out all event handlers of all elements of given class. But these handlers must be attached using jquery.

function getAllEventHandlersByClassName(className) {
  var elements = $('.' + className);
  var results = [];
  for (var i = 0; i < elements.length; i++) {
    var eventHandlers = $._data(elements[i], "events");
    for (var j in eventHandlers) {
      var handlers = [];
      var event = j;
      eventHandlers[event].forEach(function(handlerObj) {
        handlers.push(handlerObj.handler.toString());

      });
      var result = {};
      result[event] = handlers;
      results.push(result);

    }

  }
  return results;
}

// demo

$('.target').on('click',function(event){
  alert('firstClick handler')
});
$('.target').on('click',function(event){
  alert('secondClick handler')
});
$('.target').on('mousedown',function(event){
  alert('firstClick handler')
});
console.log(getAllEventHandlersByClassName('target'));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class='target'> </div>
like image 181
rishabh dev Avatar answered Sep 27 '22 22:09

rishabh dev


You can use getEventListeners() which is part of the chrome devtools but for employing client side, there's an possible-duplicate question that partially answers this: How to find event listeners on a DOM node when debugging or from the JavaScript code? which basically shows (in the second voted answer) that depending on how the events are set (javascript attribute, eventListener, jquery, other lib) there are different ways to retrieve the functions.

The Visual Event 2 program mentioned in the first question seems to be more of a library doing what the second answer is suggesting so maybe this will solve your problem.

like image 40
Anders Elmgren Avatar answered Sep 27 '22 22:09

Anders Elmgren