0

javascript functions on an event like onKeypress are not working , if i create a input tag through JAVSCRIPT using innerHTML . Below is the code for that i am using . Also the same is working if i am doing it in directly JSP file .

$('<table id="additionalPassPopupTable'+rowNo+'" class="passenger_table_center">'+
        '<tr>'+
            '<td colspan="2" style="padding-left:20px; padding-top:5px; background-color:#e1e1e1;">Passenger Information</td>'+
        '</tr>'+
        '<tr>'+
            '<td  style="padding-left:40px;">First Name *</td>'+
            '<td>'+
                '<input type="text" name="fname" >'+
            '</td>'+
            '<td>'+
                '<font color="red"></font>'+
            '</td>'+
        '</tr>'+
        '<tr>'+
            '<td  style="padding-left:40px;">Last Name *</td>'+
            '<td>'+
                '<input type="text" name="lname" ></td>'+
            '<td>'+
                '<font color="red"></font>'+
            '</td>'+
        '</tr>'+
        '<tr>'+
            '<td style="padding-left:40px;">Mobile Number</td>'+
            '<td>'+
                '<input type="text" name="phoneno" maxlength="10" onkeypress="return isNumber(event)" >'+
            '</td>'+
            '<td>'+
                '<font color="red"></font>'+
            '</td>'+
        '</tr>'+
        '<tr>'+
        '<td style="padding-left:40px;">Email Address</td>'+
            '<td>'+
                '<input type="text" name="email" maxlength="100" onkeypress="return validateEmail(event)">'+
            '</td>'+
            '<td>'+
                '<font color="red"></font>'+
            '</td>'+
        '</tr>'+
    '</table>').appendTo('#additionalPassengerDiv' );

function isNumber(evt) {
    evt = (evt) ? evt : window.event;
    var charCode = (evt.which) ? evt.which : evt.keyCode;
    if (charCode > 31 && (charCode < 48 || charCode > 57)) {
        return false;
    }
    return true;
}

function validateEmail(evt) {

    evt = (evt) ? evt : window.event;
    var charCode = (evt.which) ? evt.which : evt.keyCode;
    if (charCode > 31 && ((charCode < 48 || charCode > 57) && (charCode < 64 || charCode > 90) && (charCode < 97 || charCode > 122) && (charCode != 46) && (charCode != 45) && (charCode != 95))) {
        return false;
    }
    return true;
}
8
  • any error in your browser console Commented Jan 13, 2014 at 6:57
  • where is the test function defined and how Commented Jan 13, 2014 at 6:58
  • make sure the function test is available in the global scope Commented Jan 13, 2014 at 6:59
  • 1
    did you try $('#additionalPassengerDiv').html("<b>test</b>"); Okay this won't probably help you. Commented Jan 13, 2014 at 7:02
  • 1
    You should really consider distilling all that HTML into a template. That's a bit of a maintenance nightmare. Commented Jan 13, 2014 at 7:03

1 Answer 1

1

Hello here by I am attaching your code into jsfiddle. In this your code is working. Please find fiddle here.

Thanks. Java script function in innerhtml :JSFiddle

function isNumber(evt) {
alert("Validate Number");
evt = (evt) ? evt : window.event;
 var charCode = (evt.which) ? evt.which : evt.keyCode;
if (charCode > 31 && (charCode < 48 || charCode > 57)) {
    return false;
}
return true;}
function validateEmail(evt) {
alert("Validate Email Id");
evt = (evt) ? evt : window.event;
var charCode = (evt.which) ? evt.which : evt.keyCode;
if (charCode > 31 && ((charCode < 48 || charCode > 57) && (charCode < 64 || charCode > 90) && (charCode < 97 || charCode > 122) && (charCode != 46) && (charCode != 45) && (charCode != 95))) {
    return false;
}
return true;}

$(document).ready(function(){
$('#additionalPassengerDiv' ).html('<table id="additionalPassPopupTable" class="passenger_table_center">'+
    '<tr>'+
        '<td colspan="2" style="padding-left:20px; padding-top:5px; background-color:#e1e1e1;">Passenger Information</td>'+
    '</tr>'+
    '<tr>'+
        '<td  style="padding-left:40px;">First Name *</td>'+
        '<td>'+
            '<input type="text" name="fname" >'+
        '</td>'+
        '<td>'+
            '<font color="red"></font>'+
        '</td>'+
    '</tr>'+
    '<tr>'+
        '<td  style="padding-left:40px;">Last Name *</td>'+
        '<td>'+
            '<input type="text" name="lname" ></td>'+
        '<td>'+
            '<font color="red"></font>'+
        '</td>'+
    '</tr>'+
    '<tr>'+
        '<td style="padding-left:40px;">Mobile Number</td>'+
        '<td>'+
            '<input type="text" name="phoneno" maxlength="10" onkeypress="return isNumber(event)" >'+
        '</td>'+
        '<td>'+
            '<font color="red"></font>'+
        '</td>'+
    '</tr>'+
    '<tr>'+
    '<td style="padding-left:40px;">Email Address</td>'+
        '<td>'+
            '<input type="text" name="email" maxlength="100" onkeypress="return validateEmail(event)">'+
        '</td>'+
        '<td>'+
            '<font color="red"></font>'+
        '</td>'+
    '</tr>'+
'</table>');});
Sign up to request clarification or add additional context in comments.

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.