467,888 Members | 1,500 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,888 developers. It's quick & easy.

I generate a FORM by js,but i can't input anything?

21 function js_reply_msg(node,g_id,u_id,par_id)
22 {
23 node.innerHTML="<FORM><TEXTAREA
name=\"msg_con\"><\/TEXTAREA><br><INPUT type=\"submit\"
value=\"reply\"><\/FORM>"
24 node.nextSibling.nextSibling.innerHTML=""
25 }

My thought is that when i clicked a link,the js function generate the
Form,then get the input and deal with it.
But now ,i generate the FORM,but can't input anything:
when i press the mouse left button,i can input;if i don't ,i can't
input.

Can anyone help me?Thank you

Aug 10 '06 #1
  • viewed: 1406
Share:
1 Reply
firenet wrote:
21 function js_reply_msg(node,g_id,u_id,par_id)
22 {
Don't include line numbers in posted code, indent code properly using 2
or 4 spaces and manually wrap it at about 70 characters to help prevent
auto-wrapping.

23 node.innerHTML="<FORM><TEXTAREA
name=\"msg_con\"><\/TEXTAREA><br><INPUT type=\"submit\"
value=\"reply\"><\/FORM>"
Allowing uncontrolled addition of line breaks will usually add more
errors to your code - manually wrap it.

You can nest double-quotes inside single-quotes and vice versa, ensure
that you insert valid HTML:

node.innerHTML = '<FORM action=""><div>'
+ '<TEXTAREA name="msg_con"></TEXTAREA>'
+ '<br><INPUT type="submit" value="reply">'
+ '</div></FORM>';

That snippet, on its own, seems to 'work' just fine in IE and Firefox.

24 node.nextSibling.nextSibling.innerHTML=""
This line is meaningless in the context of the code snippet you have
posted and causes an error if I just try to run your code in a test
page. Ensure that posted code runs when copy/pasted into a test page
and doesn't display spurious messages.

Blindly navigating down the DOM tree using nextSibling is likely to
cause problems because some browsers will insert extra nodes for
whitespace, others won't. There may be more (or fewer) nodes between
siblings than you expect. You must test the nodes to see if you've
found what you expect, and deal with it if you don't.

25 }

My thought is that when i clicked a link,
What link? You haven't shown any link, or how the function is called.
If you are using an A element and javascript pseudo-protocol, that may
be your issue.
the js function generate the
Form,then get the input and deal with it.
But now ,i generate the FORM,but can't input anything:
Please use proper punctuation, this isn't a chat session.
Capitalisation is important, I shouldn't have to wonder when you've
made a typing error and when you're just being cute..

when i press the mouse left button,i can input;if i don't ,i can't
input.
So the form generates OK, but you have to click in it to give it focus
before entering any text - that is exactly how form controls work. If
you want to create the form and put focus on it, you are better off to
use DOM, then use the textarea's focus method, e.g.

<title>Reply message textarea</title>
<script type="text/javascript">

function addReplyMsg(node)
{
if (!node || !document.createElement) return;

function addEl(parent, tagName, elType){
var t = document.createElement(tagName);
if (typeof elType == 'string' && elType.length){
t.type = elType;
}
parent.appendChild(t);
return t;
}

var oForm = addEl(node, 'form');
oForm.action="";
var oDiv = addEl(oForm, 'div');
var oTA = addEl(oDiv, 'textarea');
oTA.name = 'msg_con';
addEl(oDiv, 'br');
var oSub = addEl(oDiv, 'input', 'submit');
oSub.value = 'Reply';
if (oTA.focus) oTA.focus();
}

</script>

<input type="button" value="Add"
onclick="addReplyMsg(document.getElementById('xx') );">
<div id="xx"></div>
--
Rob

Aug 10 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.