/    Sign up×
Community /Pin to ProfileBookmark

submit button not working on a dynamically created form

hi

ive created an html document dynamically with javascript and am having troubles getting the submit button to work. the code for the button is as follows:

var submit = document.createElement(“input”);
submit.setAttribute(“type”, “button”);
submit.setAttribute(“value”, “End Consultation”);
submit.setAttribute(“onClick”, “changes(p, form)”);
form.appendChild(submit)

the button shows up, but when clicked it will not run the changes(p, form) function. if i remove the ” ” around changes(p, form) it will run the function when the page loads, but still not when the button is clicked. Can anyone see what the problem is? does anyone have any suggestions?? much thanks

Migu

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@KorJun 18.2004 — onclick is not an attribute, thus you can not assign a value using setAttribute() method. Try better setEvent() or even better create the element using document.write or document.getElementById(id).innerHTML

On the other hand I hope that the variable p and form are already defined somewere.
Copy linkTweet thisAlerts:
@David_HarrisonJun 18.2004 — Try this:var submit=document.createElement("input");
submit.type="button";
submit.value="End Consultation";
submit.onclick=function(){changes(p,form);}
form.appendChild(submit);
×

Success!

Help @Mr_MiGu spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...