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.

BODYBOND
  • 117
  • 1
  • 4
  • 12

2 Answers2

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

whatever
  • 75
  • 6
  • 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. – whatever Jul 30 '21 at 14:41
  • 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 . – BODYBOND Jul 30 '21 at 20:59
  • Where do you execute your javascript code? In a dynamic action? – whatever Aug 02 '21 at 08:46
  • no, in plsql procedure using htp.p function – BODYBOND Aug 02 '21 at 08:57
  • it works using this way HTP.p ('alert("The value of v_test is ' ||v_test || '")' ); .. thanks – BODYBOND Aug 02 '21 at 10:39
  • Nice! Happy to help! Sorry If I was a bit confused its hard to help over the internet but im glad it works. – whatever Aug 02 '21 at 11:16
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