开发者

How to change the target div for an appendChild javascript

开发者 https://www.devze.com 2022-12-22 22:42 出处:网络
I am using a javascript form validator, and it creates a div for its error messages.Currently it appends the div to the html body but I would like to change it to append it to a specific div of my cho

I am using a javascript form validator, and it creates a div for its error messages. Currently it appends the div to the html body but I would like to change it to append it to a specific div of my choosing. I am new to .js but have given this a shot anyway with no result.

function inlineMsg(target,string,autohide) {
var msg;
var msgcontent;
if(!document.getElementById('msg')) {
lightbox = document.getElementById('TB_window');
msg = document.createElement('div');
msg.id = 'msg';
msgcontent = document.createElement('div');
msgcontent.id = 'msgcontent';
document.body.appendChild(msg);
msg.appendChild(msgcontent);
msg.style.filter = 'alph开发者_StackOverflowa(opacity=0)';
msg.style.opacity = 0;
msg.alpha = 0;
} else {
msg = document.getElementById('msg');
msgcontent = document.getElementById('msgcontent');
}

The div being created is the #msg div. From my un-educated mind it looks like line 10 is doing the appending. document.body.appendChild(msg)

I created line 5 the lightbox = document.getElementById('TB_window') which I think (from my limited knowledge) allows me to reference lightbox anywhere in this function. So my thought on line 10 was to do something like;

   document.lightbox.appendChild(msg);

thereby appending the msg div into the TB_window div but it isn't working... any ideas?


Remove the document prefix, so

lightbox.appendChild(msg);

This will be because lightbox is already referring to a document element, it is in effect saying

document.document.getElementById();

In the same way ATM Machine means Automatic Teller Machine Machine. (Well that's how I understand it to be anway)


lightbox is a variable, not a property of the document object:

lightbox.appendChild(msg);

If you are accessing the element only once, you don't even need the variable:

document.getElementById('TB_window').appendChild(msg);
0

精彩评论

暂无评论...
验证码 换一张
取 消