1

I'm trying to loop through all elements that contain a certain data attribute and then replace/remove certain characters.

//replace chars put in by money mask since model is double
$("input[data-input-mask='money']").each(function() {
        alert(this.value); // shows: $ 1,000
        alert('test$ ,'.replace('$ ', '').replace(',', '')); //shows: test
        this.value = this.value.replace('$ ', '').replace(',', '');
        alert(this.value); //shows: $ 1,000
});

this.value is still the original value. What might I be doing wrong here?

9
  • May be original value does not have $ and ,.. Try 'Hey$,'.replace('$','').replace(',',''); Commented Apr 29, 2016 at 18:08
  • Can you post example html for an input you are trying get with that selector? Commented Apr 29, 2016 at 18:09
  • Your code works. Please show some more context. Have you simplified your code in this question? Commented Apr 29, 2016 at 18:17
  • It's working well. jsfiddle.net/3esrgyzn Commented Apr 29, 2016 at 18:17
  • There is no $ (consider space) sub-string in your string.. Commented Apr 29, 2016 at 18:25

2 Answers 2

1

Use .localeString()

UPDATE

After rereading the OP, I realize the opposite is desired. That's still easy. Instead of using a mask, use localString(). Then it's a matter of not using localestring() when you processing the values.

SNIPPET

$("input[data-input-mask='money']").each(function() {
    var cash = parseFloat(this.value);
    var green = cash.toLocaleString('en-EN', { style: 'currency', currency: 'USD' });
    alert(green);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input data-input-mask='money' value="623.23">
<input data-input-mask='money' value="20199">
<input data-input-mask='money' value="">

Sign up to request clarification or add additional context in comments.

2 Comments

I like this but the masking that I was adding, I needed to strip out the formatting and only have a numeric value before it was submitted to the controller which I did by unbinding the mask to the input. However this solved the next issue I was looking to figure out when I wanted to display the value outside of an input field that added the symbols back in.
That's great, I'm glad that you resolved your issue and got a bonus. :-)
0

You can loop through each element and replace it like this.

  <script>
    $(document).ready(function(e) {
            //Retrieve all text of amount di;
            $.each( $('.amount'), function(){
                var unique_id = $(this).text();
   //check if any price is match to FREE THEN replace it with   NOT FREE
                 if(unique_id=='FREE'){
                        $(this).text("NOT FREE");
                 }
            });
    });
    </script>

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.