Search code examples
plsqloracle-apexprocedureapex-codeplsql-package

Pass PL-SQL variable value to JavaScript function on PL-SQL Procedure


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.


Solution

  • 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