cancel
Showing results for 
Search instead for 
Did you mean: 

JavaScript updating htmlb component value

Former Member
0 Kudos

Hello all,

I'm not a JavaScript guy atall and need some help.

I'm writing a Java DynPage iView in EP6 (not a JSPDynPage).

Can we update the value shown in an InputField from JavaScript? What I mean is, we have defined an input field and we can obviously now write

<code>

myInputField.setString("Hello");

</code>

but I want to do this from JavaScript (I do have a reason, I'm leaving it out for simplicities sake).

So I would write a JavaScript function, added to the form object as raw text....

<code>

myForm.addRawText("<script>function myFunc(){

//How can I update the input field from here?

}</script>");

</code>

If you have any ideas, or if you know that I can't do this then I'd be glad to hear from you.

Thanks,

Patrick.

Accepted Solutions (1)

Accepted Solutions (1)

SidBhattacharya
Product and Topic Expert
Product and Topic Expert
0 Kudos

For InputField you will need to use jsObjectNeeded="true"

Then add a Client side event (or leave this out if you dont this event) by doing

inputFieldID.setClientEvent(com.sapportals.htmlb.enum.EventTrigger.ON_CHANGE,"javascriptFunction()");

Then get the HTMLB inputfield ID

<%
String inputField =myContext.getParamIdForComponent(inputFieldID);
%>

You can use the eval(inputField) in javascript code to work with the control ( .setValue() in javascript will set the value for the inputfield ).

hth

Sid

Answers (3)

Answers (3)

Former Member
0 Kudos

Hello all,

Thanks for all the help.

I've moved away from that project for the time being so it'll be a whiles before I put your help to the test.

I've notified our other programmer that is still there about your answers and hopefully he'll try it out.

Thanks again everyone,

Patrick.

Former Member
0 Kudos

Hello folks,

Noel, it's a dynpage iView written in Java that I can add some JavaScript to with

<code>

myForm.addRawText("JavaScript");

</code>

not an html page with JavaScript. So the input filed is an HTMLB InputField object not an HTML input field.

Siddhartha, you mentioned using jsObjectNeeded, how is this used? The layout is not built in a JSP, I am not using a JSPDynPage... I'm using a DynPage and building the interface from java.

The actual problem is this. We have an iView that allows users to enter parameters, click 'Execute' and a client side event forces a couple of iViews to do something with those input parameters. No problems.

One of the input fields is for a parameter 'Station', there are almost 1000 stations in the system so the user can't remember all of these numbers. We have given them the ability to enter part of a station number and click 'Search'. We were opening a popup window displaying a table of all stations that contained the search pattern and then when the user selects a row from the table we wanted to pass the selected station number back to the input field. The problem is that when the popup window is active the portal page with the original iView is not active so regular eventing won't work. That's why I wanted to use JavaScript, in the popup I write something like

<code>

window.opener.myFunc(selectedStationNumber);

</code>

Having added the JavaScript function myFunc to the original input parameter iView (that opened the popup) this function will then be called. From this function I want to then put the 'selectedStationNumber' into the station number input field.

What I've done just now is place the iView displaying the stations table on the page instead of in a popup window. This way I can us client side eventing back and forward between the iViews and it works. Using a popup window will make the page tidier I think.

I suppose one solution (using the popup method) would be to put the selected station number in the HTTP session, call a JavaScript function in the original iView that forces a reload, on reload read from the HTTP session to get the selected station number. This seems quite untidy though. I'd much rather be able to simply pass the selected number straight from the popup to the iView that opened the popup.

Thanks for your replies, both of you.

Any more help with this would be great.

Patrick.

detlev_beutner
Active Contributor
0 Kudos

Hi Patrick,

ok, we've done this about 15 months ago, here comes a proposition:

We have done it with two components, one component for the popup and one for setting the value / closing the window. Anyhow, the value could be set by the first or by the second component, that does not really matter. If you don't need a server roundtrip at all, all this also could be implemented within one component.

I'll show the way with two components.

From your "underlying" page/iView, you press a button which opens a new window / component. Pass the html name of the form and the input field in question, too. Now the popup pops up the user makes a choice and presses a "Choose this" button. A server roundtrip occurs, saving the value (into a bean) and generates the "Pass value / Close window" page. This could look like this (here given as JSP):

<jsp:useBean id="SelectionBean" scope="session" class="com.btexx.example.SelectionBean"/>
<SCRIPT LANGUAGE="JavaScript">
  window.opener.document.<%=SelectionBean.getFormID()%>['<%=SelectionBean.getInputFieldID()%>'].value='<%=SelectionBean.getSelectedValue()%>';
  window.close();
</SCRIPT>

Hope it helps

Detlev

nol_hendrikx
Active Contributor
0 Kudos

Hi Patrick,

If you would like to set a field via javascript, you will need to know the field name (in your case myInputField).

Assigning values in Javascript is easy:

document.forms[0].myInputField.value = 'your new text goes here';

document is referring to your current screen. forms[0] is an entry in the forms list (first form in this case). Can be replaced with forms["myFormHasAName"].

Please note this .value property is only available at client side. So no communication with the server.

The above explains a standard Javascript solution for it. However you are mentioning you are using a Java Page. Did you mix it up with javascript? No worries then, the solution might be given by Siddhartha (above).

cheers,

Noel