0

Hi friends trying to add an input box by clicking on button and also applying onfocus and onblur functions on newly created input box for you reff plz see this link http://jsfiddle.net/2TDZA/ and also check my code mentioned below. The main problem is when i add an input the script which will be use while onfocus and onblur are stop working please help me guys

Thanks in advance.. :)

HTML

<table width="278" border="0" align="left" cellpadding="0" cellspacing="0" id="emailTable">
        <tr>
          <td width="207" align="left"><input name="" value="Enter Friend’s mobile no" type="text" onfocus="if(this.value == 'Enter Friend’s mobile no') {this.value=''}" onblur="if(this.value == ''){this.value ='Enter Friend’s mobile no'}" placeholder="Enter Friend’s mobile no" autocomplete="off"/></td>
          <td width="71" align="right" valign="top"><div class="addRow">Add</div></td>
        </tr>
      </table>

SCRIPT

 $('.addRow').click(function () {
        $('#emailTable').append("<tr><td  align='right'><input name='' value=\"Enter Friend’s mobile no\" type='text' onfocus='if(this.value == \"Enter Friend’s mobile no\") {this.value= ''}' onblur='if(this.value == ''){this.value =\"Enter Friend’s mobile no\"}' placeholder=\"Enter Friend’s mobile no\" autocomplete='off'/></td><td class='delRow'> X</td></tr>")
        })

 $(document).on("click", '.delRow', function (event) {
        $(this).parent('tr').remove();
    });
4
  • after input added onblur and onfocus stops working on newly added input Commented Jan 27, 2014 at 11:14
  • Looks like blur and focus are not set for dynamically created elements. See aruns answer below and modify the blur and focus events. Commented Jan 27, 2014 at 11:19
  • Not it's not like that. It can be set. Here the problem was simply quotes... Commented Jan 27, 2014 at 11:30
  • @FurquanKhan no the problem was also delegating event using on Commented Jan 27, 2014 at 11:36

3 Answers 3

1

Look at the code below:

 $('.addRow').click(function () {
    $('#emailTable').append("<tr><td  align='right'><input name='' value=\"Enter Friend’s mobile no\" type='text' onfocus='if(this.value == \"Enter Friend’s mobile no\") {this.value= \"\"}' onblur='if(this.value == \"\"){this.value =\"Enter Friend’s mobile no\"}' placeholder=\"Enter Friend’s mobile no\" autocomplete='off'/></td><td class='delRow'> X</td></tr>")
    })

$(document).on("click", '.delRow', function (event) {
    $(this).parent('tr').remove();
});

Or refer to the updated fiddle:

Updated

Sign up to request clarification or add additional context in comments.

Comments

0

Use jQuery event handlers instead

 $('.addRow').click(function () {
     $('#emailTable').append("<tr><td  align='right'><input name='' value=\"Enter Friend’s mobile no\" type='text' placeholder=\"Enter Friend’s mobile no\" autocomplete='off'/></td><td class='delRow'> X</td></tr>")
 })

 $(document).on("click", '.delRow', function (event) {
     $(this).parent('tr').remove();
 });
 $('#emailTable').on("blur", 'input', function (event) {
     if (this.value == '') {
         this.value = "Enter Friend’s mobile no";
     }
 });

 $('#emailTable').on("focus", 'input', function (event) {
     if (this.value == 'Enter Friend’s mobile no') {
         this.value = "";
     }
 });

Demo: Fiddle

Comments

0

Try this:

 $('.addRow').click(function () {
    $('#emailTable').append("<tr><td  align='right'><input name='' value=\"Enter Friend’s mobile no\" type='text' onfocus='if(this.value == \"Enter Friend’s mobile no\") {this.value= \"\"}' onblur='if(this.value == \"\"){this.value =\"Enter Friend’s mobile no\"}' placeholder=\"Enter Friend’s mobile no\" autocomplete='off'/></td><td class='delRow'> X</td></tr>")
    });

I have removed single quotes and added double quotes in your code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.