Skip to content

Instantly share code, notes, and snippets.

@bspavel
Last active May 4, 2018 16:13
Show Gist options
  • Save bspavel/9a7cfa2e96eb6c436b577e2d035af30b to your computer and use it in GitHub Desktop.
Save bspavel/9a7cfa2e96eb6c436b577e2d035af30b to your computer and use it in GitHub Desktop.
Add jQuery function to specific elements
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="//code.jquery.com/jquery-3.3.1.min.js"></script>
<title></title>
<script type="text/javascript">
$(function(){
$.fn.Myfunc = function(){
this.each(function(){
var className = $(this).attr('class');
$(this).html(className);
});
};
$('li').Myfunc();
});
</script>
</head>
<body>
<a href="https://stackoverflow.com/questions/4709822/add-jquery-function-to-specific-elements">
https://stackoverflow.com/questions/4709822/add-jquery-function-to-specific-elements
</a>
<br />
<ul>
<li class="id1">item 1</li>
<li class="id2">item 1</li>
<li class="id3">item 1</li>
<li class="id4">item 1</li>
<ul>
<ul>
<li class="test1">item 1</li>
<li class="test2">item 1</li>
<li class="test3">item 1</li>
<li class="test4">item 1</li>
</ul>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment