Remove Last Comma from String in jQuery

random

Remove Last Comma from String in jQuery

JavaScript Remove Last Character of a String

This blog post demonstrates how to remove last comma from string javascript. Let say I have a string value in a variable strVal, and I need to trim last comma from string. Find the below jQuery code snippet :-
remove last comma from string java,remove last comma from string in jquery,remove last comma from string c#,remove last comma from string in c# net,trim last comma,javascript remove last character of a string
trim last comma
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>remove last comma from string jquery</title>
    <script src="http://code.jquery.com/jquery-1.9.1.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {
            $('.btnRemove').click(function (event) {
                var strVal = $.trim($('.txtValue').val());
                var lastChar = strVal.slice(-1);
                if (lastChar == ',') {
                    strVal = strVal.slice(0, -1);
                }
                $("#result").text(strVal);
            });
        });
    </script> 
</head>
<body>
    <div>
        <table>
            <tr>
                <td>
                    <input type="text" class="txtValue" value="asp.net mvc, jquery," />
                </td>
                <td>
                    <button class="btnRemove">Remove</button>
                </td>
            </tr>
            <tr>
                <td>
                    <div id="result" style="color:red;"></div>
                </td>
            </tr>  
    </div>
</body>
</html>
Remove Last Comma from String in jQuery Reviewed by Ravi Kumar on 12:45 AM Rating: 5

No comments:

All Rights Reserved by Etechpulse © 2012 - 2017

Contact Form

Name

Email *

Message *

Powered by Blogger.