Alan Carr
Alan Carr

Reputation: 322

Adding Placeholder To Input Field By Name

I'm trying to add a placeholder to an an attribute using JavaScript.

The Input element's HTML is:

<td class="gfield_list_cell gfield_list_72_cell3" data-label="Amount Paid">
<input type="text" name="input_72[]" value="" tabindex="67">
</td>

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.

<script type="text/javascript">
  var input = document.getElementsByName('input_72[]')[0];
  input.setAttribute('placeholder', '$');
</script>

Any help would be appreciated. Thanks

Upvotes: 2

Views: 8254

Answers (4)

Mahipal Reddy M
Mahipal Reddy M

Reputation: 1

Try this:

$(input[name="input_72[]"]).setAttr('placeholder', '$');

use jquery to add placeholder dynamically

Upvotes: 0

Mihai Alexandru-Ionut
Mihai Alexandru-Ionut

Reputation: 48327

You should use querySelector method.

document.querySelector('.gfield_list_cell.gfield_list_72_cell3 input').setAttribute('placeholder','$');
<table>
<td class="gfield_list_cell gfield_list_72_cell3" data-label="Amount Paid">
       <input type="text" name="input_72[]" value="" tabindex="67">
</td>
</table>

Upvotes: 1

Onix
Onix

Reputation: 2179

Try this:

var input = document.getElementsByName('input_72[]')[0];
input.setAttribute('placeholder', '$');

Upvotes: 1

sayyednoman
sayyednoman

Reputation: 9

No need to using javascript simply add placeholder in input.

<input type="text" name="input_72[]" value="" tabindex="67" placeholder="Name">

Upvotes: -2

Related Questions