Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8922507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:51:13+00:00 2026-06-15T06:51:13+00:00

A JS Fiddle of the plugin working A contraction plugin for numerical values (ST,RD,TH).

  • 0

A JS Fiddle of the plugin working

A contraction plugin for numerical values (ST,RD,TH).

I have been playing around with creating my own plugin, at the moment I’m just getting used to the syntax etc, this is just my playing with a script I needed and found today.

ok – my plugin should add th, st, rd etc on the end of any number with a span.num wrapped around it, the problem is for every number i need to call the plugin each time?

so I have two numbers, i need to give them both unique class names and then call my plugin (see fiddle)

my question: is it possible to only use one class name many times, and get back different results for each?

I’m thankful in advance for any replies and all apologies for my English today – brain fried!

    $(document).ready(function(){
        $(".num1").simpleContraction();
        $(".num2").simpleContraction();
    });


    (function( $ ){
        $.fn.simpleContraction = function() {

          var num = this.text();
          var len = num.length, last_char = num.charAt(len - 1), abbrev

            if (len == 2 && num.charAt(0) == '1') {
                abbrev = 'th'
            } else {
                if (last_char == '1') {
                    abbrev = 'st'
                } else if (last_char == '2') {
                    abbrev = 'nd'
                } else if (last_char == '3') {
                    abbrev = 'rd'
                } else {
                    abbrev = 'th'
                }
            }

            return this.html(num+abbrev);
        };
    })( jQuery );
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T06:51:15+00:00Added an answer on June 15, 2026 at 6:51 am

    You need to wrap a this.each() block around your code so that each element in the jQuery collection is processed in turn:

    $.fn.simpleContraction = function() {
        return this.each(function() { 
            // NB: "this" is now a DOM element - wrap if required
            var $this = $(this);
            ...
        });
    }
    

    That said, I wouldn’t actually write this as a true plugin – it’ll have really nasty side effects if called on the wrong elements.

    Instead, you can take advantage of the fact that .text can be passed a function that is passed the original text out of an element, and replaces it with the return value of that function:

    $.simpleContraction = function(index, text) {
        // parse "text" etc
        ... 
        return num + abbrev;
    }
    
    // call on required elements
    $('span.num').text($.simpleContraction);
    

    The advantage of this approach is that it separates the DOM manipulation from the string manipulation, and gives you a function that can be used on any string variable, and not just ones stored in the DOM.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Fiddle: http://jsfiddle.net/MhWm5/16/ I have some dynamically generated table rows/values with dynamic IDs <td class=control-group>
I'm playing around with a JQuery plugin called JRumble: http://jackrugile.com/jrumble/ I setup a simple
So here's a working example of my situation: fiddle The pic is just the
I'm working on a type of 'spotlight' plugin that puts four boxes around a
I have the following fiddle which includes a snippet of the HTML I'm working
see fiddle Firstly i am new to jquery. i have to make one cell
see fiddle i have html table and one textbox and one button.make cell selection
JS Fiddle Link http://jsfiddle.net/Xfvpu/1/ Okay so I have a document with xhtml doctype and
I have following fiddle: http://jsfiddle.net/BFSH4/ As you see there are two issues: The h1
I'm using the jQuery validation plugin (http://bassistance.de/jquery-plugins/jquery-plugin-validation/), and it's working on most everything except

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.