CA Service Management

 View Only

 CA Service Catalog JavaScript Focus on a Form Field

Scott Stineman's profile image
Scott Stineman posted Apr 08, 2024 10:24 PM

Hello: In a CA Service Catalog form script, is it possible to focus on a particular field?  I have a text-field that is not required, except, under specific conditions.  I tried using the standard JS function, document.getElementById("myTextField").focus(); .  I put the text-field ID in place of "myTextField".  I get an "undefined" error.  Is there another way of setting the focus to field in CA Service Catalog?  Thank you.  Scott

Attachment  View in library
Lindsay Estabrooks's profile image
Lindsay Estabrooks

Perhaps a better idea would be to validate the field (making it required) in a function called by your onSubmit.

Scott Stineman's profile image
Scott Stineman

@Lindsay Estabrooks: Thank you for the response.  I am still new to JavaScript and working with events with CASC forms.  I do not want the field in question to be required all-the-time, only during certain situations.  For example, I have address fields including a separate text-field for floor and room number.  I also have fields for ordering various items (computers, desk phones, network accounts, and so on).  The floor and room number is an optional field, unless a desk phone is ordered.

How would I handle that situation using the onValidate event and a JavaScript function?  Thank you.  Scott

Satya Kiran Jasti's profile image
Broadcom Employee Satya Kiran Jasti

Hi Scott,

As Lindsay has suggested, we can use validation mechanism. We can use onvalidate attribute of text field as below (example form has a checkbox, if it is checked then text field cannot be empty)

{

    onvalidate: function(_val){
     if(ca_fdIsSelectedCheckBox(ca_fd.formId,'chb_1') && _val ==""){
       return "This field is required if desk phone is requested";
     }
   }

}

Text Fields onvalidate: ca_fd.js.onvalidate(_val)

But the catch here is even if the checkbox is unchecked after the text field is marked as required, it still will be marked as required until text field is filled or atleast one character is typed which will trigger our onvalidate script again. 

Regards,

Satya Kiran

Scott Stineman's profile image
Scott Stineman

Satya Kiran: This almost works the way that I want.  It prevents the submit action, however, the focus does not move to the empty text field.  If the validation fails, the ideal situation is to move the cursor over to the blank text-field for "floor/room".  Do you have any other suggestions?  Scott