user3033437 user3033437 - 5 months ago 46
Ajax Question

call to a javascript function from a dynamically added html button fails when I pass params

First I add a menu with selectable items

<div id='form'><select id="mySelect" multiple="multiple" onclick="myFunction()">
<option value="1">Please select a Form type</option>
<option value="2">Second</option>
<option value="3">Third</option>
<option value="4">Fourth</option>

If I chosse item number two there is some javascript code that runs and add html code dinamically to the page. As you can see there is a submit button in the html code, that calls another javascript function, which is setType();

<script type="text/javascript">
var foo = document.getElementById('mySelect');
var param1;
var param2;
var param3;
function myFunction(){
if (foo)
if (foo.selectedIndex != null )

if(foo.selectedIndex == '1'){
//cargamos formulario 1

var newElement ="<div id='div1'>Advertiser ID<input type='text' name='Avertiser' value='Advertiser'><br><input type='submit' value='Submit advertiser' onClick='setType('Advertiser')';></div><div id='div2'>Offer: <input type='text' name='Offer' value='Offer'><br><input type='submit' value='Submit Offer'></div><div id='div3'>Date: <input type='text' name='Date' value='Date'><br><input type='submit' value='Submit Date'></div>";
var bodyElement = document.body;
bodyElement.innerHTML = newElement + bodyElement.innerHTML;
document.body.insertAdjacentHTML("" );
// ("Advertiser Id: <input type='text' name='AD' value='AD'><br><input type='submit' value='SubmitAD'>Offer: <input type='text' name='Offer' value='Offer'><br><input type='submit' value='SubmitOffer'>Date: <input type='text' name='Date' value='Date'><br><input type='submit' value='Date'>");
if(foo.selectedIndex == '2'){
//cargamos formulario 2

if(foo.selectedIndex == '3'){
//cargamos formulario 3


My problem is I want to pass the Advertiser Value as a param into this javascript function, to store it in a global variable, and when the three submit buttons params are in their variable, then make an ajax call to another file. My problem is when I call the javascript function with the param, it doesn't run. Without params works. I don't understand the problem.

function setType(id){

Answer Source

check your "" :) 'setType('Advertiser')' is bad code, try


and in HTML ALWAYS use "" in atributes, not ''!!!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download