0

I have an application on Oracle Apex 20.2 with oracle DB 18c. and I have a pl-sql procedure that render questions and some specific actions. now I want to enhance the actions using JavaScript functions but my problem is :

For example I have this procedure plsql code :

  if cq.type != 'MULTIPLE' then
 htp.p('<select name ="select1" onchange="showme()">' || c_crlf);



 htp.p('<input type=radio name="' || l_name || '" id="' || l_id || '_x" class="rb" ' || l_required ||
                            'value=""><span class="icon rb"></span><label for="' || l_id || '_x" class="hideMeButHearMe">' || encode(cq.other_label) || '</label>');
htp.p('<script>
                       function showme(){
                       var s = document.form1.select1;
                       var h = document.form1.input1;
                       if( s.selectedIndex == '||l_id||'  ) {
                       h.style.visibility="visible";
                       }else{
                       h.style.visibility="hidden";
                       }}
                        </script>');

l_id variable value works fine with html code and I could use the value to do actions. but when try to use l_id value on JS function, JS can't read the value.

Could anyone help what is the correct way to pass a plsql variable value to a JS function on the same procedure.

2 Answers 2

1

So I tried this out myself and:

declare

    v_test varchar2(200) := 'yes';

begin

    HTP.p ('<script type="text/javascript">');
    HTP.p ('alert("The value of  v_test is ' ||v_test || '")' );
    HTP.p ('</script>');

end;

results in:

enter image description here

If you want to use javascript in your pl sql you have to make sure your pl sql is executed AFTER HEADER in Apex:

enter image description here

Sign up to request clarification or add additional context in comments.

6 Comments

If you want to use pl slq and javascript combined while the page has loaded and the user triggers it by some interaction you can also save the PLSQL value into an Apex Item. Make sure to submit it in your Dynamic Action "Page items to submit". After that you can use the apex.item(":P100_YOUR_ITEM_NAME").getValue()); function and work with it in JS. The Apex Item can be set to hidden.
Thanks.. but I tried this method this way in my JS code : if( s.selectedIndex == apex.item( "P30_JS_VALUE" ).getValue() ) etc.... it gives me error Apex is not defined ,, and I put the variable P30_JS_VALUE to the page items to submit. could you help please .
Where do you execute your javascript code? In a dynamic action?
no, in plsql procedure using htp.p function
it works using this way HTP.p ('alert("The value of v_test is ' ||v_test || '")' ); .. thanks
|
0

You can use APEX_JAVASCRIPT package, e.g. APEX_JAVASCRIPT.ADD_INLINE_CODE or APEX_JAVASCRIPT.ADD_ONLOAD_CODE procedures.

Something like this:

APEX_JAVASCRIPT.ADD_INLINE_CODE(APEX_STRING.FORMAT('const x = "%s"', 'x value'))

Some other functions from this package can be also useful for prevent XSS, escape special symbols etc.

https://docs.oracle.com/en/database/oracle/application-express/20.2/aeapi/APEX_JAVASCRIPT.html

1 Comment

Thanks .. could you give me an example

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.