How to count and make change in all Elementid

Asked

Viewed 39 times

-1

What this code allows is to modify the value of the field from 0.00 to the desired value, even if it is 2, it replaces for 2.00.

My problem is that it only replaces 1 INPUT, if you have 2 INPUTS with the same ID it only modifies 1.

How do I modify them all with the same Elmt ID.

<script>

document.getElementById('currency').onblur = function (){    
    //number-format the user input
    this.value = parseFloat(this.value.replace(/,/g, ''))
                    .toFixed(2)
                    .toString()
                    .replace(/\B(?=(\d{3})+(?!\d))/g, '.');
    //set the numeric value to a number input
    document.getElementById('number').value = this.value.replace(/,/g, '')
} 

  • 1

    If you’re going to have one more input then I suggest using a class so that you don’t have two elements with the same id.

  • how so? what I intend to find is all the same ID and modify them...

  • 1

    @John is not possible. You may even have several inputs with the same ID, but Javascript will consider only one of them.

  • you can even select all elements with the same id this way: Document.querySelectorAll("#id");

  • doesn’t work. I replace it with Document.querySelectorAll('#currency'). onblur =Function(), and nothing

  • Using my code, you can help me right?

  • Okay, I’ll see what I can do

  • if the answer does not solve your problem edit your question by also inserting the html snippet, so that I can better understand the structure of your page

  • 2

    Brotherzinho, an id is like a CPF, each one has its own single. If you are using equal id’s in more than one element, it is a sign that you need to learn HTML. Ever wonder if more than one person had the same number, what the hell wouldn’t it be? :D

Show 4 more comments

1 answer

-2


using getElementById("id") it will return only the first element with id, so to select all elements with same id we can use querySelectorAll("#id"), which will return all elements with that id.

let elements = document.querySelectorAll("#currency");

after selecting all the elements we can use the foreach to add the event in each element in this way:

   elements.forEach(el => {
        el.onblur = () => {
            //number-format the user input
            el.value = parseFloat(el.value.replace(/,/g, ''))
                .toFixed(2)
                .toString()
                .replace(/\B(?=(\d{3})+(?!\d))/g, '.');
            //set the numeric value to a number input
            el.value = el.value.replace(/,/g, '');
        };
    });

remembering that it is not recommended to have more than one element with the same id in an html file, I believe that you could identify the elements using a class and get the elements this way:

let elements = document.querySelectorAll(".currency");

So you could pass a foreach on Elements in the same way.

html used for testing:

<td>
    <tr>
        <input name='product_cost[]' placeholder='0.00' class='currency' type='text' class='form-control'
            placeholder='0.00' required>
    </tr>

</td>
<td>
    <input name='product_pvp[]' placeholder='0.00' class='currency' type='text' class='form-control' placeholder='0.00'
        required>
</td>
<td>
    <input name='product_pvp[]' placeholder='0.00' class='currency' type='text' class='form-control' placeholder='0.00'
        required>
</td>
<td>
    <input name='product_pvp[]' placeholder='0.00' class='currency' type='text' class='form-control' placeholder='0.00'
        required>
</td>
  • even so, still not giving

  • <td><input name='product_cost[]' placeholder='0.00' id='currency' type='text' class='form-control' placeholder='0.00' required></td> <td><input name='product_pvp[]' placeholder='0.00' id='currency' type='text' class='form-control' placeholder='0.00' required></td>

  • ok I made some changes to your html-based code and this working, I will edit the answer.

  • right. works using class. now a question... and if I use MULTIPLES of <tr>... it no longer works, only in the first <tr> Hehe

  • try to give a console.log(Elements); probably it should not be finding the elements, I added more fields and put one of them inside a <tr> and keeps working. see if there are any errors in the console. I will add the html I used for the tests

  • it works, but this must be my mistake, because the <tr> added are not REAL, that is, not in the code, the user to which Add them via Buttao.

  • understood, so it probably doesn’t work because when Document.querySelector runs that <tr> doesn’t exist yet, you could create a function (or a method if you’re working with OO) and call the function after this <tr> is added to also add this new element in the Elements variable

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.