SmartForm: Set TextBox value by Javascript - when focussed, it gets cleared?!

  • 11 August 2016
  • 6 replies

Badge +1



I have added a value to a TextBox using Javascript. Now I want to use this value for Parameters. Apparently I cannot. Although there is a value in the TextBox it iseems I cannot use it. When I click into the TextBox then it gets cleared. If I manually input something I can use it afterwards.


I am using this statement 

<script>document.getElementsByName("urlTextBox")[0].value = "abc"</script>


Any ideas?




6 replies

Badge +4


In SmartForms the values for all the controls are actually stored in an xml file in a Javascript variable.  I won't comment on the architecture of this, but there is a way to actually set the value of a SF textbox using Javascript.   Here is the function that we have in production now:


function CustomUpdateSFControl(ControlName, Value) {

var myTextBoxXPath = 'Controllers/Controller/Controls/Control[@Name="' + ControlName + '"]';
var windowToUse = window;

if (!checkExists(windowToUse.viewControllerDefinition)) {
windowToUse.viewControllerDefinition = $xml(windowToUse.__runtimeControllersDefinition);

var myControl = windowToUse.$sn(windowToUse.viewControllerDefinition, myTextBoxXPath);
var controlInfoObj = new windowToUse.PopulateObject(null, Value, myControl.getAttribute('ID'));
windowToUse.executeControlFunction(myControl, 'SetValue', controlInfoObj);
windowToUse.raiseEvent(myControl.getAttribute('ID'), 'Control', 'OnChange');


As you can see there are some other things happening here to really inform K2 that the value has changed and set the value back into the Xml that K2 uses to store the data.


You'll need to add this function using an HTML Literal control or some other custome control.  Unfortunatly, this JS is too big for a single DataLabel

We only have this running for TextBoxes, but it might work for other values also.


Good luck,



Badge +7

I notice the note about text being too large for a data label but we have found a work around for that. It also is easier to fix when a view cannot be edited because of something you added in a control (had that happen to me). We created a smartbox to store Label values you just need two collumns 1) ID and 2) memo field. Create a view to add and edit records for this smart box. Create your record with the text you need.


In the form intialize rule add a smartobject call to the smart box with the input of the ID you want and use the output to put the memo field into the data label. We first started using this to put large blocks of formated text on a form like click-wrap agreements and I started using it for jave script after I trashed a view so bad it would not open in edit. Now if I do that I just go delete the text from the smartbox record and my problem is gone. The advantage to using it for large blocks of formated text is that we also found our business users decided to change things almost as soon as we promoted a new version to production.


This may or may not be helpful.

Badge +4

I'm searching for JS solution for replacing special characters with undescore in datalabel.


The upper JS is great, since it work, but it does only setting defined value. I would need:

- read value from data label

- replace special characters

- save corrected back in data label


I appriciate any help.

Userlevel 3
Badge +5

CustomUpdateSFControl is a great piece of code to push things into the model with Javascript but I'm stumped on how to get an event out of the model. I have a view that executes a SmartObject Method and updates a control on the view. I need to trap that update and respond to it. The problem is updating a value with code doesn't trigger a change event. Is there any event I can listen for a SmartObject list event? I tried listening to ajaxComplete but got nothing.


Unearthing this topic :-)

This piece of code works great with input/textarea but I’m not able to make it work with dropdown. Does anyone know if there’s an alternative ?


Unearthing this topic :-)

This piece of code works great with input/textarea but I’m not able to make it work with dropdown. Does anyone know if there’s an alternative ?

I found the answer to my own question. The code works fine but the Value passed needs to be an integer.



CustomUpdateSFControl(ControlName, parseInt(Index)) 

works fine. To find the index matching the plain text Value from the list:



Additionally, it can be tricky to find the name of the Control for the DropDown with an access only to the front end. To do so, inject the following code

var ControlID='id of the select'

 var myTextBoxXPath = 'Controllers/Controller/Controls/Control[@ID="' + ControlID + '"]';

 var windowToUse = window;

var myControl = windowToUse.$sn(windowToUse.viewControllerDefinition, myTextBoxXPath);