5

Add the number spacing with jQuery

 2 years ago
source link: https://www.codesd.com/item/add-the-number-spacing-with-jquery.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Add the number spacing with jQuery

advertisements

I would like to inspect a span element and check for its number format and apply digit grouping to it with jquery

It the number has more than 3 digits, that is for >1000, it must do digit grouping, to separate the thousands from the hundreds, and the hundred thousands from the thousands:

<span>5500000.00</span>

should become

<span>5 500 000.00</span>

Is that possible with jQuery?


There are number of formatting plugins for this task. Or use can use regular expression.

function formatPrice(price) {
    return price.reverse().replace(/((?:\d{2})\d)/g, '$1 ').reverse();
}

// Need to extend String prototype for convinience
String.prototype.reverse = function() {
    return this.split('').reverse().join('');
}

Tests:

formatPrice('1234.00')   // "1 234.00"
formatPrice('12345.00')  // "12 345.00"
formatPrice('123456.00') // "123 456.00"

Maybe there is a way to do this without reverting a string?

Related Articles

Add the option dynamically with jquery and update the html source

I have two listbox, A & B: when I double click on an item of A , the item will be added in List B. And i have done this. List B visually show the value, but when I go to the view source of the page, I dont see new <option>. This is my List A opt

How to detect the scrolling position with jquery unrelated to scrolling events?

I'm creating a mockup elevator, but there are two things causing trouble. Updating .floor-number to reflect the current floor. It seems I should change .floor-number with the content property like so: .content-property { visibility: hidden; } .conten

How to add different values ​​for the same CSS property in the same element with jQuery?

I am trying to add the same CSS property multiple times with different values each time for the same selector, with jQuery. $('.element').css({ 'cursor':'-moz-grab', 'cursor':'-webkit-grab', 'cursor':'move' }); How can I do this, without each time ov

Delete the row of a table and add it to another with jQuery

I am trying to remove a row from one table and add it to another with jQuery. I've examined this similar Stack Overflow post and feel like I am doing roughly the same thing but am obviously missing something as what I am doing is not working. I know

How to initialize and control the web form with jQuery

I have a simple web form with 2 elements: <form> <input id="number" name="number" maxlength="1" type="text" /> <textarea id="description" name="description"></textarea> &l

How to set the data attribute with jQuery on the item that is not yet in DOM?

How can I set the data-attribute with jQuery on an element that is not in the DOM yet? Code: var panelHeading = $('<div/>', {class:'panel-heading', href:'#'+username+'PanelContent'}); panelHeading.data('toggle', 'collapse').data('target',"#&quo

Phone number format with jquery & regex done

Possible Duplicate: telephone number format with jquery&regex i need to verify and convert any input val into telephone number format, i.e input er+f375g25123435s67 i need to convert into +375 25 1234567 a most suitable code is: $('input').live({ key

How to add the Splash screen with the progress bar in Webview Android Studio

How to add Splash screen with Progress bar in Webview android studio I am newbie to the android development help me to add the splash screen with progress bar activity_main.xml: <?xml version="1.0" encoding="utf-8"?> <Relati v

How to change the button class after calling the asp method with jQuery?

i have aspx web page that contains 500 button (input type="button") that they creates in run time. Each button has this class: button button-green , and i call the click method with jQuery. My question is : how can i change the class of the butt

In Excel, add 1 if not the number, or add the number

In excel, how do I write a formula that does the following: In a certain cell range, if the cell contains a non-numeric entry, add one, or else add the number the cell contains. for example 4 xyz def bc 2 0 =9 or 1 ab cd 2 af =6Contents of the data A

Delete the dom code if the mobile browser with jQuery

Is this possible? I mean, I know you can use display: none; with media queries, but that does not prevent the nodes to be loaded, which is resource intensive. I don't want to hide them, I want to remove them. That's why I used this: function removeit

how to change the value of the text box with jQuery?

I would like to change the value of the textboxes when the user clicks the submit button with Jquery. I tried it with $('textbox').val('Changed Value'); and $('#dsf').val('Changed Value'); but neither method worked. <!doctype html> <html lang=&qu

How to add a dropdown list next to the filter box with Jquery data?

I am using Jquery datatables (http://datatables.net/) for all grids on my asp.net pages. When the bFilter is enabled a search filter is displayed. I would like to add a dropdownlist (to select a specific column to apply filter) next to it? How do I a

How to add a different style with jquery in the same div name?

how to add diferrent style in same div name with jquery? example like this. <div id="link"> <div class="one">This is first link</div> <div class="one">This is second link</div> <div class=&quo

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK