cancel
Showing results for 
Search instead for 
Did you mean: 

Access HTMLB controll in Javascript

Former Member
0 Kudos

Hi,

I am tring to access an HTMLB controll (InputField) from JavaScript, but having some problems...

I have created a DynPage and created an InputField: InputField inField = new InputField("inField");

Now, in the javascripts I am trying to access this InputField text with:

var txt = document.form.inField.value

but it does not work... I get a JavaScript error.

Any suggestion how to access the InputField from the JS? maybe with getElementByid?

Thanks in advance,

Aviad

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi Aviad,

The reason for the error is that the actual html id of an htmlb component is not the id you give it in the c'tor.

You can get the "real" id using:

this.getPageContext().setCurrentForm(myForm);
String id = this.getPageContext().getParamIdForComponent(inField);

Then, in the javascript use this id the same way you tried to use "inField":

form.addRawText("var txt = document.form."+id+".value;");

If that doesn't work try:

form.addRawText("var txt = document.getElementById('"+id+"').value;");

Hope that helps,

Yoav.

Former Member
0 Kudos

HI Yoav,

You have given him a workaround solution. The code i posted above is the solution recommended by SAP.

Prakash

Former Member
0 Kudos

Hi Prakash,

I am trying to do the same but it is not working. the code is not getting executed beyond this line.

func = window[funcName];

it is coming as undefined. Is ther any solution for this.

Thanks in advance

Kiran

Answers (1)

Answers (1)

Former Member
0 Kudos

you need to do the following.

function validateField()
{
var funcName = htmlb_formid+"_getHtmlbElementId";
func = window[funcName];
var inputfield = eval(func("fname_input"));
var inputString = inputfield.getValue().toString();
}

Former Member
0 Kudos

Hi Parkash,

Can you please explain what each line in your code does?

Thanks for your help,

Aviad

Former Member
0 Kudos

Hi Aviad,

Htmlb controls are not a direct reprsentation of html controls. The way to get hold of them is by following lines.

function validateField()
{
//htmlb_formid : this is automatically generated 
//the following lines will get you the javascript function
//name to call to get hold of your input field
var funcName = htmlb_formid+"_getHtmlbElementId";
func = window[funcName];
//The funtion is called with id of the input field 
//which returns the htmlb control object
var inputfield = eval(func("fname_input"));
//this will get you the value of inputfield
var inputString = inputfield.getValue().toString();
}

<b>Note: Make sure you set jsObjectNeeded = true for the htmlb control.</b>

PS: Please consider rewarding points for helpful answer. thank you.

Message was edited by: Prakash Singh