I’m trying to add a placeholder to an an attribute using JavaScript.
The Input element’s HTML is:
JavaScript
x
4
1
<td class="gfield_list_cell gfield_list_72_cell3" data-label="Amount Paid">
2
<input type="text" name="input_72[]" value="" tabindex="67">
3
</td>
4
Seems I need to target it by the class also of the parent td “gfield_list_cell gfield_list_72_cell3”
I’m trying to target this using Javascript and a $ sign as the placeholder. I’m using this, but can’t get it work.
JavaScript
1
5
1
<script type="text/javascript">
2
var input = document.getElementsByName('input_72[]')[0];
3
input.setAttribute('placeholder', '$');
4
</script>
5
Any help would be appreciated. Thanks
Advertisement
Answer
You should use querySelector
method.
JavaScript
1
1
1
document.querySelector('.gfield_list_cell.gfield_list_72_cell3 input').setAttribute('placeholder','$');
JavaScript
1
5
1
<table>
2
<td class="gfield_list_cell gfield_list_72_cell3" data-label="Amount Paid">
3
<input type="text" name="input_72[]" value="" tabindex="67">
4
</td>
5
</table>