Borrowing Code from Other Libraries - J Query

You found a useful function in another JavaScript library and want to use the same technique in your jQuery code. In this case, it’s the .radioClass() method from the Ext Core library, which adds a class to the matching element(s) and removes the same class from all siblings of the matching element(s).
Given this HTML:

you’d like to run code like this:

You may even want to allow a “multiple-select” radio class:


Write a simple plugin to add the .radioClass() method to jQuery:

This is a short enough function that it’s not too hard to follow as a one-liner, but indenting the code as described in Recipe 5.4 makes it completely clear how it works:


The composer Igor Stravinsky is reported to have said, “Good composers borrow; great composers steal.” He apparently stole the quote from T.S. Eliot, who wrote, “Immature poets imitate; mature poets steal.”
Good ideas come from many places, and other JavaScript libraries are chock-full of good code and ideas. If there is code in another open source library that you can use or that you can translate to work with jQuery, you’re free to do that—if you respect the other author’s copyright and license.

You may not even need the actual code in a case like this one, where the implementation is very simple and just the idea of having a “radio class” method is the missing link.While not required, it’s a good courtesy to give credit to the source of the idea.Whether the idea comes from elsewhere or is something you thought of yourself, in a surprising number of cases you can write a useful jQuery plugin in one or a few lines of code.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd Protection Status

J Query Topics