dimanche 21 mars 2021

extend jquery function with custom method

I want to make an extend method that will make content of my code ("hello world") to red color. The #target is not getting initialized by defualt.

$('#target').makeRed();
jQuery.fn.extend({
  makeRed: function() {
    var text = $(this).css('color', 'red');;
    return text;
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="target">Hello World</div>



Aucun commentaire:

Enregistrer un commentaire