Avatar of Eduardo Aviles
Eduardo Aviles
Flag for United States of America asked on

Passing JScript value to VbScript

I have built an HTA application with JScript language and VbScript. I am trying to pass the value of JScript variable to VbScript, but I am stuck. Can you please help.  Here is the JScript code: (I need to pass the docNum [ ] and rev [ ] values, as there may be multiples.)

<script type="text/javascript">
        
            var room = 1;
            function add_fields() {
                  room++;
                  var objTo = document.getElementById('room_fileds')
                  var divtest = document.createElement("div");
                  divtest.innerHTML = '<div class="content"><span>Document Number: <input type="text" style="width:150px;" name="docNum[]" value="" /></span><span>Rev: <input type="text" style="rev:20px;" name="rev[]" value="" /></div>';
                  
                  objTo.appendChild(divtest)
                  
            }
            
      </script>

<script language="VBScript">
      Option Explicit

      Sub ProcessInfo_OnClick
      
       End Sub
</script>
Thank you
JScriptVB Script

Avatar of undefined
Last Comment
Sam Jacobs

8/22/2022 - Mon
Sam Jacobs

I haven't created any HTA apps, but what I do for my ASP.Net apps is to have JavaScript call the file again, passing the values in the QueryString (e.g. http://FQDN/filename.ext?docnum=xxxx&rev=yyyy). Then, have the VBScript code retrieve the values from the QS.
Eduardo Aviles

ASKER
Thank you Sam, I had attempted something similar by putting all the strings in an input delimited by ',' but the users did not want that. They wanted dynamically to have the UI add a new input row by the means of a plus sign. That's what I have done which the user can add as many rows as they want on the UI, now I need to pass the collection so that I can parse it. Since the users may have multiple documents to process.
Sam Jacobs

Hi Eduardo ... Do you need to go back to VBScript?
You can't do what you need staying in JavaScript?
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Eduardo Aviles

ASKER
Hello Sam, 99% of the code is already built in VbScript and am up against a deadline and I am not an expert in Javascript. If I can just get how to display the docNum[] values that I can loop thru them then I can figure out the rest. I tried this with no success:

function myfunction() {
      
                  var myStringArray = [docNum];
                  var arrayLength = myStringArray.length;
                  for (var i = 0; i < arrayLength; i++) {
                        alert(myStringArray[i]);
                  }
                  }
Eduardo Aviles

ASKER
I changed the code around to getElementById("content") and now I get an error of index[undefined]: undefined

<script type="text/javascript">
        
            
            
            var d;
            function add_fields() {
                  d = document.getElementById("content");
                  
                  d.innerHTML += "<br /><span>Document Number: <input type='text'style='width:150px;'value='' /></span> <span>Rev: <input type='text' style='width:150px'  value='' /> </span>";
      
            }
            

      
            function myfunction(item, index) {
                  
                  d.innerHTML = d.innerHTML + "index[" + index + "]: " + item + "<br>";
            }
            

      </script>
Sam Jacobs

Sorry, Eduardo... just saw this.
I'll be in front of my PC in an hour or so, and I'll check it out.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Sam Jacobs

Hi Eduardo,

First, the reason that you can't pass values from JavaScript to VBScript is that VBScript code is parsed before JavaScript, so that once JavaScript is being executed, there's no going back to evaluate VBScript.

While I haven't written any HTA applications, I have never seen brackets in an HTML input field (name="docNum[]").
If docNum is an array, where is it getting its values from?

Sam
Eduardo Aviles

ASKER
Its getting the values from an input from the user.. Here is the full code complete. Sorry about that.  The UI allows the user to select the plus sign and add another row to enter more input.

<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title></title>

      <link rel="stylesheet" type="text/css" href="/css/normalize.css">

      <link rel="stylesheet" type="text/css" href="/css/result-light.css">

  <style type="text/css">
    div { padding:5px;}
  </style>
 
 
      <script type="text/javascript">
        
            var d;
            function add_fields() {
                  d = document.getElementById("content");
                  
                  d.innerHTML += "<br /><span>Document Number: <input type='text'style='width:150px;'value='' /></span> <span>Rev: <input type='text' style='width:150px'  value='' /> </span>";
      
            }
            

      
            function myfunction(item, index) {
                  
                  d.innerHTML = d.innerHTML + "index[" + index + "]: " + item + "<br>";
            }
            

      </script>
      
<script language="VBScript">
      Option Explicit

      Sub ProcessInfo_OnClick
      
      
       End Sub
      
      Sub Message
      
            Msgbox docNum
      
      
       End Sub
</script>

</head>




<body>
 
         <div>
                  <div id="content">
                        <span>Document Number: <input type="text" style="width:150px;" name="docNum[]" value=""/>
                        <span>Rev: <input type="text" style="rev:20px;" name="rev[]" value="" />   <input type="button" id="more_fields" onclick="add_fields();" value="+" />
                  </div>
         </div>

 
 
  <tr>
      <td>
            <input type="button" value="Submit" onclick="myfunction()">
      </td>
  </tr>
 
 
</body>
</html>
Sam Jacobs

Eduardo,

Thanks for the source ... it helps a lot.
I stand corrected ... in an .hta, it appears that you CAN execute JavaScript from VBScript.
I'm going to look at this some more for a bit and get back to you shortly.

Sam
Your help has saved me hundreds of hours of internet surfing.
fblack61
Eduardo Aviles

ASKER
Thank you Sam, I really appreciate the help.
ASKER CERTIFIED SOLUTION
Sam Jacobs

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Eduardo Aviles

ASKER
Good Morning Sam,
    Thank you so much for the code. I have tried it and I am getting an error when selecting the + sign "Object doesn't support property or method 'getElementsByClassName"  on this line: var docs = document.getElementsByClassName("doc");
Sam Jacobs

Hmmm. Strange. I just uploaded the file that I was using. Let me double-check it.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Sam Jacobs

Hi Eduardo,

I just downloaded the file from the link above, and it seems to work fine.
Did you copy and paste it the contents from your screen or download the file (Save Link As...)?

Sam
Eduardo Aviles

ASKER
I did a copy and saved it on the application, let me try it again
Eduardo Aviles

ASKER
Hello Sam,
     I retried it and it exactly what I need. Since it is HTML there are some differences to HTA as that's why it was failing before, but I can figure it out.  Thank you so much for your help, as the code is working perfectly.


Thank you again.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Eduardo Aviles

ASKER
Hello Sam, Thank you again for the code it works as expected. The code is well written and am able to use this in multiple applications. I really appreciated your time and knowledge.  Thank you again.
Sam Jacobs

Hi Eduardo,

You are most welcome.

Sam