Page 1 of 1

javascript not working

Posted: Sat Sep 25, 2010 6:01 pm
by kolmes911
This is my code, and it does not create any element. Anybody please tell me why.
I've tried figuring out for a week or so.

Code: Select all

<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<script language="javascript">
	var logout=document.createElement('input');
	logout.setAttribute('type', 'button');
	logout.setAttribute('value', 'Log out');
	logout.setAttribute('onclick', 'window.location="URL"');
	document.getElementsByTagName('body')[0].appendChild(logout);
	
	var bb=document.createElement('div');
	bb.setAttribute('id','bdiv');
	bb.appendChild(spn);
	bb.appendChild(logout);
	document.getElementsByTagName('body')[0].appendChild(bb);
	
	var inpu=document.createElement('input');
	inpu.setAttribute('type','text');
	inpu.setAttribute('name','message');
	inpu.className='inputbox';
	
	var sendbu=document.createElement('input');
	sendbu.setAttribute('type','submit');
	sendbu.setAttribute('value','Send');
	sendbu.onclick=sendMsg();
	
	var msgfo=document.createElement('form');
	msgfo.appendChild(inpu);
	msgfo.appendChild(sendbu);
	
	var sb=document.createElement('div');
	sb.setAttribute('id','sdiv');
	sb.appendChild(msgfo);
	document.getElementsByTagName('body')[0].appendChild(sb);
</script>
</body>
</html>

Re: javascript not working

Posted: Sat Sep 25, 2010 7:44 pm
by McInfo
The variable spn is undefined.

Code: Select all

bb.appendChild(spn);
The function sendMsg() is undefined.

Code: Select all

sendbu.onclick=sendMsg();
If sendMsg() is defined, do you mean

Code: Select all

sendbu.onclick=sendMsg;
Appending an element to a different parent moves that element. The logout element is taken from the body element and given to the "bdiv" element.

Code: Select all

document.getElementsByTagName('body')[0].appendChild(logout);
bb.appendChild(logout);

Re: javascript not working

Posted: Sun Oct 03, 2010 1:25 am
by kolmes911
Thank you, McInfo.

You were absolutely right.

Sorry for this late reply.