/    Sign up×
Community /Pin to ProfileBookmark

button onclick not working?

[CODE]var submitbutton=document.createElement(“input”);
submitbutton.type=”button”;
submitbutton.value=”Login”;
submitbutton.onclick=loginsubmit;
ul4.appendChild(submitbutton);[/CODE]

i have tried setting onclick to loginsubmit, loginsubmit(), “loginsubmit”, “loginsubmit()”, “javascript:loginsubmit()”…none of it is working. What am I doing wrong?

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@TcobbJan 02.2011 — I suspect that your script is firing before whatever corresponds to the var 'ul4' comes into existence from being loaded. Consider the following:

[CODE]<html>
<head>
<script type = "text/javascript">
window.onload = function(){
var ul4 = document.getElementById("col");
var submitbutton=document.createElement("input");
submitbutton.type="button";
submitbutton.value="Login";
submitbutton.onclick=loginsubmit;
ul4.appendChild(submitbutton);
}

function loginsubmit(){
alert("WORKING");
}
</script>
</head>
<body>
<div id = 'col'>
</div>
</body>
</html>[/CODE]
Copy linkTweet thisAlerts:
@alfehaieotasdhauthorJan 02.2011 — hmm im not exactly sure what you mean by that..here's more of my code: this is inside a modalbox, so the function login() is called when a button is clicked. I know it may be hard to answer without looking at the modalbox code, but I wouldn't think it should matter since the button is contained in the div included in the modalbox. I can include a copy of the html produced from firebug if that would help.

[CODE]function login(){
var div1=document.createElement("div");
var ul4=document.createElement("ul");
var submitbutton=document.createElement("input");
submitbutton.type="button";
submitbutton.value="Login";
submitbutton.onclick=loginsubmit;
ul4.appendChild(submitbutton);
div1.appendChild(ul4);
Modalbox.show(div1, {title: "Login"});
}

function loginsubmit(){
//CODE
}[/CODE]


but loginsubmit is not getting called when submitbutton is clicked.

Thanks
Copy linkTweet thisAlerts:
@thraddashJan 02.2011 — Try using the two functions in another order...

[CODE]function loginsubmit(){
//CODE
}

function login(){
var div1=document.createElement("div");
var ul4=document.createElement("ul");
var submitbutton=document.createElement("input");
submitbutton.type="button";
submitbutton.value="Login";
submitbutton.onclick=loginsubmit;
ul4.appendChild(submitbutton);
div1.appendChild(ul4);
Modalbox.show(div1, {title: "Login"});
}[/CODE]
Copy linkTweet thisAlerts:
@alfehaieotasdhauthorJan 02.2011 — tried that. it didn't help
Copy linkTweet thisAlerts:
@thraddashJan 02.2011 — Did this test, with the code you gave...

[code=html]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<script type="text/javascript">

function loginsubmit(){
alert('HERE');
}

function login(){
var div1=document.createElement("div");
var ul4=document.createElement("ul");
var submitbutton=document.createElement("input");
submitbutton.type="button";
submitbutton.value="Login";
submitbutton.onclick=loginsubmit;
ul4.appendChild(submitbutton);
div1.appendChild(ul4);

document.body.appendChild(div1);
}

</script>

</head>
<body>

<script type="text/javascript">
login();
</script>

</body>
</html>[/code]


Worked the way I expected it too, seems the issue is within the Modalbox.
Copy linkTweet thisAlerts:
@alfehaieotasdhauthorJan 02.2011 — interesting. thanks
Copy linkTweet thisAlerts:
@alfehaieotasdhauthorJan 03.2011 — Does anyone know of any other way of creating an inline popup? I want to create a login page for my website, but i don't want it to redirect from what the user is currently viewing, and I would rather it not be an annoying popup.

And if there is no simple way to do this, what other ideas do people have for login? This is for a website that doesn't require a login, so I don't want it to bug people unless they click the login button.

Thanks
×

Success!

Help @alfehaieotasdh 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 5.28,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...