Expand my Community achievements bar.

Can you condense my 4 lines of code into 1 line?

Avatar

Former Community Member

I'm writing a passive validation script that validates fields with the Enter & Exit events and changes the background colour and border colour of just the field's value:

this.ui.oneOfChild.border.fill.color.value = "255,216,229";

this.ui.oneOfChild.border.getElement("edge", 0).color.value = "244,138,159";

this.ui.oneOfChild.border.getElement("edge", 1).color.value = "244,138,159";

this.ui.oneOfChild.border.getElement("edge", 2).color.value = "244,138,159";

this.ui.oneOfChild.border.getElement("edge", 3).color.value = "244,138,159";

This is working fine my question is how can I condense these four lines into one line of code?

this.ui.oneOfChild.border.getElement("edge", 0).color.value = "244,138,159";

this.ui.oneOfChild.border.getElement("edge", 1).color.value = "244,138,159";

this.ui.oneOfChild.border.getElement("edge", 2).color.value = "244,138,159";

this.ui.oneOfChild.border.getElement("edge", 3).color.value = "244,138,159"; 

I'm using this on every single field, and it's starting to make my code swell.

Thanks!

4 Replies

Avatar

Former Community Member

To answer your question, here's a way:

var i=5;while(--i) this.ui.oneOfChild.border.getElement("edge", i).color.value = "244,138,159";


However if you're using this in every single field, you should either isolate the code into a function inside a script objects, and simply call it with 'this' as parameter:

myScriptObject.ChangeColor(this);

Or, if you're using LC Designer ES, place the code in an object further up the hierarchy and activate "Enable Event Propagation" (check box next to "Language: Javascript" "Run At: Client")

Avatar

Former Community Member

Cheers mate, that worked great.

I was hoping you'd bite on to what I'm trying to achieve as well, which you have. I'm using LiveCycle Designer ES 8.2 however can't for the life of me find the Enable Event Propagation option? Searched on Google as well which returned the same instructions, select the check box next to Run At in the script editor - I just don't have this option available?

I have 30 or so fields, and want to execute a function on Enter & Exit events, a simple background & border change dependant on empty values (simple passive validation). Currently I'm using the Enter & Exit events for each element explicitly, there must be an easier way to do this right? Is it possible to loop through all required fields in the form and assign functions to each elements Enter & Exit event?

I'm typically a web developer, as pseudo code I'm after something like this:

for(var i = 0; i < length of fields that are required; i++){

     field[i].onblur = function(){

          assign function         

     }

      field[i].onfocus = function(){

          assign function         

     }

}

Any help would be greatly appeciated!

Coming from JavaScript I'm struggling to understand Livecycle a bit...

Thanks,

Avatar

Former Community Member

That enable event propogation was only added in version 9 of the Designer.

Paul

Avatar

Level 3

I know this is an older post, but I am working on something similar and the following loop worked for me:


    // change border color:


    for (i = 0; i < 4; i++) {


          this.ui.oneOfChild.border.getElement("edge", i).color.value = "255,0,0";


    }



Notes:

  • Borders are only 4 items (top, right, bottom, left). Since the count starts at "0" the loop should stop at 4 (thus "i smaller than 4" )
  • Replace "this" with whatever field reference.