Add content to existing div

Q

question.boy

I have been trying to find an example of how-to add html text to an
exisiting div and have had little success.

The best I found so far was

var pTag = document.createElement("p");
pTag.innerHTML = noBlanks+" is a mandatory field.";
document.getElementById("FormErrors").appendChild(pTag);

However, I was hoping I could pass HTML code and not just text,
something more like

"New line added.<br />"

or anyother HTML Tag I may need to use.

Also, the code only seems to work in IE and not Firefox...?

Thank you for your help!

QB
 
J

Joost Diepenmaat

I have been trying to find an example of how-to add html text to an
exisiting div and have had little success.

The best I found so far was

var pTag = document.createElement("p");
pTag.innerHTML = noBlanks+" is a mandatory field.";
document.getElementById("FormErrors").appendChild(pTag);

However, I was hoping I could pass HTML code and not just text,
something more like

"New line added.<br />"

or anyother HTML Tag I may need to use.


You can.
Also, the code only seems to work in IE and not Firefox...?

Ther error is somewhere else. This should work in FF; most browsers
support innerHTML, even though it's not officially standard.
 
J

Jeff Bigham

Assuming your div has an id attribute called "div_id", you can do
this:

var div = document.getElementById('div_id');
div.innerHTML = "<p><b>Whatever</b> HTML that you may want.</p>";

-Jeff
 
Q

question.boy

I have been trying to find an example of how-to add html text to an
exisiting div and have had little success.
The best I found so far was
var pTag = document.createElement("p");
pTag.innerHTML = noBlanks+" is a mandatory field.";
document.getElementById("FormErrors").appendChild(pTag);

However, I was hoping I could pass HTML code and not just text,
something more like
"New line added.<br />"
or anyother HTML Tag I may need to use.

You can.
Also, the code only seems to work in IE and not Firefox...?

Ther error is somewhere else. This should work in FF; most browsers
support innerHTML, even though it's not officially standard.



Here is the function in question. It works fine in IE but nothing in
Firefox.

function validate_form(theForm) {
/*Validates the user submitted data to ensure that data has been
input and that it is valid
and posts an error message if there is an issue.*/
var frmOk = true; //initialize validation status variable
var errColor = 'ffff99'; //background color when there is an error
in validation

// Create an array of controls to ensure that it wasn't left blank
var noBlanks = [ "FirstName", "LastName", "Address", "City",
"Province", "PostalCode", "TelResidential",
"TelBusiness", "TelCell", "Email", "Job"];

for ( i=0; i < noBlanks.length; i++ ){
if (isEmpty(document.getElementById(noBlanks).value)) {

document.getElementById(noBlanks).style.backgroundColor=errColor;

var pTag = document.createElement('p');
pTag.innerHTML = noBlanks+" is a mandatory field.";
document.getElementById('FormErrors').appendChild(pTag);
frmOk = false;
}
}

if (frmOk == false) {
document.getElementById('FormErrors').style.display='block';
return false;
} else {
document.getElementById('FormErrors').style.display='none';
return true;
}
}

Any idea where I have made a mistake?

Thank you for your help,

QB
 
Q

question.boy

I have been trying to find an example of how-to add html text to an
exisiting div and have had little success.
The best I found so far was
var pTag = document.createElement("p");
pTag.innerHTML = noBlanks+" is a mandatory field.";
document.getElementById("FormErrors").appendChild(pTag);

However, I was hoping I could pass HTML code and not just text,
something more like
"New line added.<br />"
or anyother HTML Tag I may need to use.

You can.
Also, the code only seems to work in IE and not Firefox...?

Ther error is somewhere else. This should work in FF; most browsers
support innerHTML, even though it's not officially standard.


Might I ask you, what is officially the standard approach to doing
what I am wanting to do?
QB
 
G

Gregor Kofler

(e-mail address removed) meinte:
Here is the function in question. It works fine in IE but nothing in
Firefox.

function validate_form(theForm) {
/*Validates the user submitted data to ensure that data has been
input and that it is valid
and posts an error message if there is an issue.*/
var frmOk = true; //initialize validation status variable
var errColor = 'ffff99'; //background color when there is an error
in validation

// Create an array of controls to ensure that it wasn't left blank
var noBlanks = [ "FirstName", "LastName", "Address", "City",
"Province", "PostalCode", "TelResidential",
"TelBusiness", "TelCell", "Email", "Job"];

for ( i=0; i < noBlanks.length; i++ ){
if (isEmpty(document.getElementById(noBlanks).value)) {

document.getElementById(noBlanks).style.backgroundColor=errColor;



Why gEBI? I suppose getElementsByName would be more appropriate. Due to
an "odd" implementation IE also populates the id property of elements
with names with the name value.

But it's IE only.

Gregor
 
G

Gregor Kofler

(e-mail address removed) meinte:

Please don't quote signatures. And "- Show quoted text -". Get yourself
a decent newsreader like Thunderbird.
Might I ask you, what is officially the standard approach to doing
what I am wanting to do?

The usual methods for manipulating the DOM: createElement(),
append/replace/removeChild(), ...

Gregor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top