Search code examples
javascriptjqueryonblur

Get the text entered in the text box on loss of focus with jQuery


How can I get the value entered in the textbox below?

<tr>
    <td class="TextBold" >1.</td>
    <td class="TextBold" >Content for Question 1:</td>
    <td class="TextBold" >
      <input name="ctl00$DefaultContent$ctl2" type="text" class="TextNormal" />
    </td>
  </tr>

This gives me the name of the textbox:

var idT = $("td:contains('1.')").next().next().children().eq(0).attr('name');

Now I would like to write function which will give the value entered in the textbox.

Something like:

 $('input[name^=ctl00$DefaultContent$ctl2]').blur(function () {
        alert($(this).val());    
    });

This worked for me:

 $('input[name="' + idT + '"]').blur(function () {
            alert($(this).val());    
        });

Solution

  • $('input[name="ctl00$DefaultContent$ctl2"]').val() should give you the value of the text field but is "ctl00$DefaultContent$ctl2" really the name?

    See this jsFiddle