Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Kaustubh's avatar

How to Populate Two Text Box Having same ID

<input type="text" id="roleid" />
<input type="text" id="roleid" />


<select name="test" id="test">
        <option value="A">One</option>
        <option value="B">Two</option>
</select>

$(document).ready(function() {
    $('select[name="role"]').on('click', function() {
        var optionID = $(this).val();
        var textBox = document.getElementById("roleid");
        if(stateID) {
            textBox.value = optionID;
        }
    });
});

In above case only one textbox is populating. I want to populate both textbox on same id, because in my program the text box is generating using foreach.

0 likes
2 replies
topvillas's avatar

Don't give two elements the same id. Use a class instead.

1 like
Kaustubh's avatar
Kaustubh
OP
Best Answer
Level 2

Thanks @topvillas it works for me

$(document).ready(function() {
    $('select[name="role"]').on('click', function() {
        var stateID = $(this).val();
        var textBox = document.getElementsByClassName('abc');
        if(stateID) {
            for(var i = 0; i < textBox.length; i++){
                textBox[i].value=stateID;
            }
        }
    });
});


<input type="text" class="abc" name="roleid" id="roleid">
1 like

Please or to participate in this conversation.