jQuery Selector How to - Select element by onclick event








Question

We would like to know how to select element by onclick event.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src='http://code.jquery.com/jquery-1.9.1.js'></script>
<script type='text/javascript'>
$(window).load(function(){<!--   w ww .  j a  va 2 s .c o m-->
    $('div[onclick^="location.href"]').css('color', 'red');
});
</script>
</head>
<body>
  <div onclick="location.href='https://en.wikipedia.org/';"> Content Here
  </div>
  <div>/* Content Here as well */</div>
</body>
</html>

The code above is rendered as follows: