Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-24-2005, 04:04 PM   PM User | #1
boob
New to the CF scene

 
Join Date: Sep 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
boob is an unknown quantity at this point
insertBefor - insertAfter Problem

I have Javascript for check Form working perfectly.
After submit the Form, The script place an Image befor each missing rquired field and place
errormessage befor submit button. The script use insertBefore to do so.

I want to place the image after each missing required field.
I change insertBefore to inserAfter and now no image after each missing required field.
After replacing insertBefore with inserAfter - no image befor or after each missing required field.

The script is working with " insertBefore" and not working with "insertAfter".
What I am doing wrong?
Below is the scipt: (red is inserBefor which I replace with insertafter)
--------------------------------------------------------------------------

</style>
<script type="text/javascript">
function checkform(of)
{
// Test if DOM is available and there is an element called required
if(!document.getElementById || !document.createTextNode){return;}
if(!document.getElementById('required')){return;}

// Define error messages and split the required fields
var errorID='errormsg';
var errorClass='error'
var errorMsg='Please enter or change the fields marked with a ';
var errorImg='img/alert.gif';
var errorAlt='Error';
var errorTitle='This field has an error!';
var reqfields=document.getElementById('required').value.split(',');

// Cleanup old mess
// if there is an old errormessage field, delete it
if(document.getElementById(errorID))
{
var em=document.getElementById(errorID);
em.parentNode.removeChild(em);
}
// remove old images and classes from the required fields
for(var i=0;i<reqfields.length;i++)
{
var f=document.getElementById(reqfields[i]);
if(!f){continue;}
if(f.previousSibling && /img/i.test(f.previousSibling.nodeName))
{
f.parentNode.removeChild(f.previousSibling);
}
f.className='';
}
// loop over required fields
for(var i=0;i<reqfields.length;i++)
{
// check if required field is there
var f=document.getElementById(reqfields[i]);
if(!f){continue;}
// test if the required field has an error,
// according to its type
switch(f.type.toLowerCase())
{
case 'text':
if(f.value=='' && f.id!='email'){cf_adderr(f)}
// email is a special field and needs checking
if(f.id=='email' && !cf_isEmailAddr(f.value)){cf_adderr(f)}
break;
case 'textarea':
if(f.value==''){cf_adderr(f)}
break;
case 'checkbox':
if(!f.checked){cf_adderr(f)}
break;
case 'select-one':
if(!f.selectedIndex && f.selectedIndex==0){cf_adderr(f)}
break;
}
}
return !document.getElementById(errorID);

/* Tool methods */
function cf_adderr(o)
{
// create image, add to and colourise the error fields
var errorIndicator=document.createElement('img');
errorIndicator.alt=errorAlt;
errorIndicator.src=errorImg;
errorIndicator.title=errorTitle;
o.className=errorClass;
o.parentNode.insertBefore

// Check if there is no error message
if(!document.getElementById(errorID))
{
// create errormessage and insert before submit button
var em=document.createElement('div');
em.id=errorID;
var newp=document.createElement('p');
newp.appendChild(document.createTextNode(errorMsg))
// clone and insert the error image
newp.appendChild(errorIndicator.cloneNode(true));
em.appendChild(newp);
// find the submit button
for(var i=0;i<of.getElementsByTagName('input').length;i++)
{
if(/submit/i.test(of.getElementsByTagName('input')[i].type))
{
var sb=of.getElementsByTagName('input')[i];
break;
}
}
if(sb)
{
sb.parentNode.insertBefore(em,sb);
}
}
}
function cf_isEmailAddr(str)
{
return str.match(/^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/);
}
}
</script>
-------------------------------------------------------------------------
boob is offline   Reply With Quote
Old 09-25-2005, 11:19 AM   PM User | #2
scrypter
New Coder

 
Join Date: Sep 2005
Location: New Zealand
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
scrypter is an unknown quantity at this point
I don't believe there is an "insertAfter" method in the DOM. You may have to use insertBefore on a cell (or whatever) that itself is after where you want the image to appear (if that makes sense!).

Paul
__________________
ScrypTik Javascript editor with built in syntax error checking
scrypter is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:23 PM.


Advertisement
Log in to turn off these ads.