J.Z. J.Z. - 7 months ago 36
Javascript Question

JQuery, get input id

<script>
$(document).ready(function() {
$('#hotelo_div :input').onblur = validate(1, this.id, "abcd");
});
</script>

<div id="hotelo_div">
<table id="hotcalcmain">
<tr>
<td>PMI1</td>
<td class="hotcc"><input type="text" id="PMI_1" maxlength="2" size="2" > %</td>
<td class="hotcc"><input type="text" id="PMI_2" maxlength="2" size="2"> %</td>
<td class="hotcc"><input type="text" id="PMI_3" maxlength="2" size="2"> %</td>
<td class="hotcc"><input type="text" id="PMI_4" maxlength="2" size="2"> %</td>
</tr>
...


How to send input id(e.g. PMI_1, PMI_2, ...) to function validate for parameter at posiont "this.id"?

Answer

$('#hotelo_div :input').onblur should be assigned a function reference rather than the returned value of a function. Also this will not point to the input here. If you are using jquery, then you can use the jquery event handlers itself.

replace this line

$('#hotelo_div :input').onblur = validate(1, this.id, "abcd");

with

$('#hotelo_div :input').blur(function(){
  validate(1, this.id, "abcd");
})