0

How can replace arabic number with latin number as that doesn't change number into div:style and doesn't change tag and ... ? How can done it?

DEMO: http://jsfiddle.net/mCUvT/

<div class="myGrid">
    <b style="top: 50px;">123456789-10-11-12</b>
    <br />
    <span style="margin: 20px 0 10px 5px">05-10-1390</span>
    <a href="http://blog.jsfiddle.net/" style="left: 20px">our blog</a>
</div>​

var grid= $(".myGrid");
grid.html(grid.html().replace(/0/g,"&#1776;"));
grid.html(grid.html().replace(/1/g,"&#1777;"));
grid.html(grid.html().replace(/2/g,"&#1778;"));
grid.html(grid.html().replace(/3/g,"&#1779;"));
grid.html(grid.html().replace(/4/g,"&#1780;"));
grid.html(grid.html().replace(/5/g,"&#1781;"));
grid.html(grid.html().replace(/6/g,"&#1782;"));
grid.html(grid.html().replace(/7/g,"&#1783;"));
grid.html(grid.html().replace(/8/g,"&#1784;"));
grid.html(grid.html().replace(/9/g,"&#1785;"));

1
  • Try jsfiddle.net/skram/mCUvT/10/ for iframe issue. I think it is better off to simplye ignore iFrame contents. Apr 5, 2012 at 20:25

1 Answer 1

1

Okay, It took sometime to crack down the decoding part.. but it worked out well.. I think you can't convert just replace the whole html, instead you need to parse down each #textNode and convert them. See below recursive function which checks for textNode and converts them.

DEMO

DISCLAIMER: Please note that the below code is a draft version and definitely a lot of improvements can be done.. I will leave it to you or other SO users to correct me on that..

CODE:

var numConversion = {
    '0': '&#1776;',
    '1': '&#1777;',
    '2': '&#1778;',
    '3': '&#1779;',
    '4': '&#1780;',
    '5': '&#1781;',
    '6': '&#1782;',
    '7': '&#1783;',
    '8': '&#1784;',
    '9': '&#1785;'
};

$(document).ready(function() {
    var grid = $(".myGrid");

    var gridContent = grid.contents();
    convertText(gridContent);

    function convertText(contents) {
        contents.each(function() {
            if (this.nodeType == 3) {
                this.nodeValue =
                $("<div/>").html(convert(this.nodeValue)).text();
            } else if (this.nodeType == 1) {
                convertText($(this).contents());
            }
        });
    }

    $(".myGrid").html(grid.html());

    function convert(txt) {
        var resultTxt = '';
        var textChar = '';
        for (var i = 0; i < txt.length; i++) {
            textChar = txt.charAt(i);
            if (numConversion.hasOwnProperty(textChar)) {
                resultTxt += numConversion[textChar];
            } else {
                resultTxt += textChar;
            }
        }

        return resultTxt;
    }
});
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.