Prasita Mukherjee Prasita Mukherjee - 1 year ago 93
HTML Question

Update placeholder by id using jQuery

I have an input field in html which is created 'dynamically' in a function say :

'<input id="id1'+var1+'" name="id1" type="text" placeholder="Provide IP"/>'

In the same function in javascript, I'm trying to change the placeholder text by :


It doesn't work!!The array arr also gets updated in the function
Also further options like :


doesn't work. It is retaining previous placeholder text only!! How to resolve this? I'm using Google Chrome

Answer Source

Your are missing to do the change after the dom is loaded. Try this:

  $("#id1").attr("placeholder","IP changed"); 

$(document).ready(function() {
  var i = 0;
  var j = i - 1
  var myArray = ["firts text", "second text", "and so on"]
  $("#button").click(function() {
    if (i > 2) {
      i = 0;
    $('input').each(function() {
      if ($(this).attr('placeholder') == "Provide IP" || $(this).attr('placeholder') == myArray[j]) {
        $(this).attr("placeholder", myArray[i]);
        return false;
    if (j > 2) {
      j = i - 1;
<script src=""></script>
<input id="id1" name="id1" type="text" placeholder="Provide IP" />
<input type="button" id="button" value="Click me" />

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download