Jump to content

problem in Passing arduments in javascript function


ammu412

Recommended Posts

In my program

 

function change_host(number,host){

alert(number+"---"+host)

}

function test(){

var num=1;

var hostF="host";

newin.innerHTML =  "<div id='er_clone"+num+"' style='display:inline;font-weight:bold;color:#FF2323;font-size:15px;'></div><center><table class='tblheaderbg' width=200><tr><th>Server Name</th><th>VM Machine Name</th><th>Vm Domain Name</th><th>VM IP</th><th>VM Net Mask</th><th>VM Gateway</th><th>VM DNS</th><th>Clone from template</th></tr><tr><td><select name='host_name"+num+"' id='host_name"+num+"' onChange='change_host("+num+","+hostF+");'>";

}

 

 

In this when i'm calling change_host() function it says  host is not defined. I don't know why it's happening......

Please give your valuable reply...........

Link to comment
Share on other sites

The problem is in your "creation" of the onChange event. The code will produce a trigger that looks something like this:

 

onChange = 'change_host(1,host);'

 

Where 15 is the value of the variable of num and host is the value of the variable hostF. Passing a number like that is fine. But, if 'host' is supposed to be a string then it needs to be properly formatted as such.

 

Try this:

function change_host(number,host){
  alert(number+"---"+host)
}
function test(){
  var num=1;
  var hostF="host";

  var htmlCode = '<div id="er_clone"'+num+'" style="display:inline;font-weight:bold;color:#FF2323;font-size:15px;"></div>';
     htmlCode += '<center><table class="tblheaderbg" width="200"><tr>';
     htmlCode += '<th>Server Name</th>';
     htmlCode += '<th>VM Machine Name</th>';
     htmlCode += '<th>Vm Domain Name</th>';
     htmlCode += '<th>VM IP</th>';
     htmlCode += '<th>VM Net Mask</th>';
     htmlCode += '<th>VM Gateway</th>';
     htmlCode += '<th>VM DNS</th>';
     htmlCode += '<th>Clone from template</th>';
     htmlCode += '</tr><tr>';
     htmlCode += '<td><select name="host_name'+num+'"';
     htmlCode += ' id="host_name'+num+'"';
     htmlCode += ' onChange="change_host('+num+', \''+hostF+'\');">';

  newin.innerHTML = htmlCode;
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.