Call Javascript method not working in Ajax onChange

I have a field called {TotAvalItems} which I want to update after select field {Product} is changed.

{TotValItems} is a label field and so for some reason it does not change once the page has been loaded. I was tired of troubleshooting that so I am trying to change the html using a javascript call in Product_on_Change ajax event. I have:

$javascript_function = ‘setvals’; // Javascript function name
$javascript_parameters = array( // Javascript function parameters, add as many as needed
{TotAvailItems});

// Call javascript function
//sc_ajax_javascript($javascript_function, $javascript_parameters);

Inside setvals I have:

document.getElementById(“id_ajax_label_totavailitems”).innerHTML = cs;

(cs is the parameter passed)

This is also not working. Can anyone help?

?

// Call javascript function
//sc_ajax_javascript($javascript_function, $javascript_parameters);

Wish it was that simple. I pasted code after changes made for troubleshooting. The code does not work when uncommented; so do you have any experience with this?

I had some problems, in a form in insert mode, with fields calculated via Ajax onChange
The calculated values were visually displayed on the form when the Ajax onChange event was triggered.
But when the form was saved they vanished and were not saved in the DB table.
While I had no problem when the form was in edit mode of an existing record.

As a workaround, in the onBeforeInsert event I added a call to the PHP function performing the actual calculations. That PHP function was the same I called in the Ajax onChange event. So no need to duplicate the function, just needed to execute it again just before SC was inserting the record in the DB table.
Issue solved.
Note: the calculated fields were text fields, disabled in insert\edit mode.

Not sure this applies in any way to your issue.
If it does, I can provide more details and the code I used.

EDIT
what about the space inside the element id?

“id_ajax_label_totavailite ms”

EDIT2
did you check if {TotAvailItems} has any value before passing it to sc_ajax_javascript macro?
For that you can use sc_ajax_message inside the ajax event.
To me it looks strange that your passing {TotAvailItems} as that is the field you’re trying to set with your code, right?

Something went wrong with the paste: there is no space in the element id reference.

Yes I checked and it has a value in it and saves into the database. The problem is it does not update on the screen so the user can get confused. javascript use was a work-around since it was not working. That’s why I wrote “{TotValItems} is a label field and so for some reason it does not change once the page has been loaded. I was tired of troubleshooting that so …”

Your use case is somewhat different but I’ll take a closer look and try to wrap my head around it. Thank you

For fields to be calculated\changed only via ajax events, instead of labels I prefer to use normal fields (text, select, integer, etc.), disabled in insert\update,
They are more easily aligned and formatted, as they retain all SC field properties.
And apart for the workaround I had to use when inserting new records (see my previous post), I had no problems with screen updates.