www.Inmagic.com    Inmagic Forums    Inmagic Forums  Hop To Forum Categories  Scripting    Javascript: copy the whole contents of one field to another field
Go
New
Find
Notify
Tools
Reply
  
-star Rating Rate It!  Login/Join 
Posted
Hello

Could anyone help regarding tweaking one of the Sample Javascripts that is available in DBText help. The code that DBText gives you (Sample JavaScript Script 5) allows you to copy the first word of one field to another field (if the second field is empty) when the record is saved.

I would like to copy the whole contents of one field to another field if the second field is empty when the record is saved.

Apologies if this is really simple but I have tried tweaking the last part of the script but can't get it work but I'm not familiar with Javascript.

Any ideas? I have reproduced the script below (including my field names)

Many thanks
Claire



function onRecordSave()
{
var srcBox = Form.boxes("ExpertType");
if (srcBox) // be sure there is a box with the given name
{
var srcText = srcBox.content;
if (srcText != "") // proceed only if the first box is nonempty
{
var tgtBox = Form.boxes("ExpertTypeAll");
if (tgtBox && tgtBox.content == "") // second box must exist and be empty
{
var allWords = srcText.split(" "); // allWords is an array of words
tgtBox.content = allWords[0];
}
}
}
}
 
Posts: 2 | Location: Bristol, UK | Registered: Wed August 17 2005Reply With QuoteEdit or Delete MessageReport This Post
Posted Hide Post
For the last if statement, use:

  
if (tgtBox && tgtBox.content == "") {
  tgtBox.content = srcText;
}


Peter Tyrrell, MLIS
Director and Lead Developer
Andornot Consulting Inc.
http://andornot.com/about/developerblog
http://twitter.com/andornot
 
Posts: 188 | Location: Victoria, BC, Canada | Registered: Thu September 20 2001Reply With QuoteEdit or Delete MessageReport This Post
Posted Hide Post
Brilliant - the script is now doing just what I needed.

Thanks very much much Peter - you're a star!

Claire
Big Grin

p.s I have reproduced the whole script below just in case any other non-javascript experts need it in future.

function onRecordSave()

{
var srcBox = Form.boxes("ExpertType");
if (srcBox) // be sure there is a box with the given name
{
var srcText = srcBox.content;
if (srcText != "") // proceed only if the first box is nonempty
{
var tgtBox = Form.boxes("ExpertTypeAll");
if (tgtBox && tgtBox.content == "") // second box must exist and be empty
{
tgtBox.content = srcText;
}
}
}
}
 
Posts: 2 | Location: Bristol, UK | Registered: Wed August 17 2005Reply With QuoteEdit or Delete MessageReport This Post
  Powered by Eve Community  
 

www.Inmagic.com    Inmagic Forums    Inmagic Forums  Hop To Forum Categories  Scripting    Javascript: copy the whole contents of one field to another field