Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

How to pass JS value in context variable

jitendrak
Level 2
Level 2

Hi All,
I have the below query.

i have created a web form using html activity (not V5) , in the web form I am collecting some data in text form, now i want to store this value in context variuable.

For eg 

Context variable created in web application with name : my_purchase_number

In the web form there is a input text field where user enters the purchase number stired in JS variable inside html, let's say variable name is purchase_number.

When user clicks on submit button i want the value of context variable ctx.vars.my_purchase_number = purchase number.

Any help on how to achieve this is highly appreciated.

 

Regards

Jay

 

1 Accepted Solution
Manoj_Kumar_
Correct answer by
Community Advisor
Community Advisor

Hello @jitendrak 

 

You will have to create a javascript function to do this. 

 

Here is an example

<script>
function updateCtxValues(){
  var purchaseNumber=document.getElemebtById('purchase_number').value;
  document.controller.setValue('/ctx/vars/my_purchase_number', purchaseNumber);
}
</script>

You will have to call this function on the form submission by onclick event

<input type="button" onclick="updateCtxValues()" value="submit">

Also, make sure your purchase number input has an id attribute of value "purchase_number"

 

Let me know if that works.

Thanks

View solution in original post

3 Replies
Manoj_Kumar_
Correct answer by
Community Advisor
Community Advisor

Hello @jitendrak 

 

You will have to create a javascript function to do this. 

 

Here is an example

<script>
function updateCtxValues(){
  var purchaseNumber=document.getElemebtById('purchase_number').value;
  document.controller.setValue('/ctx/vars/my_purchase_number', purchaseNumber);
}
</script>

You will have to call this function on the form submission by onclick event

<input type="button" onclick="updateCtxValues()" value="submit">

Also, make sure your purchase number input has an id attribute of value "purchase_number"

 

Let me know if that works.

Thanks

View solution in original post

jitendrak
Level 2
Level 2
<!DOCTYPE html> <html lang=""> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Some test form</title> <!-- Latest compiled and minified CSS --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"> <!-- jQuery library --> &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">&lt;/script&gt; <!-- Popper JS --> &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js">&lt;/script&gt; <!-- Latest compiled JavaScript --> &lt;script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js">&lt;/script&gt; <style> .top-declaration { font-size: 1.5rem; margin-top: 20px; } .submit { background-color: #5288de; color: white; font-size: 1.2rem; width: 100%; padding-top: 10px; padding-bottom: 10px; } .submit-disabled { background-color: lightgray; color: darkgrey; font-size: 1.2rem; width: 100%; padding-top: 10px; padding-bottom: 10px; } .matter { font-size: 1.2rem; text-align: justify; } .point { font-weight: 400; text-align: justify; margin-bottom: 30px; } .point:hover { color: coral; } </style> </head> <body style="" class=""> <div class="container"> <div class="row"> <div class="col-sm-12 top-declaration">Please Enter your purchase ID</div> </div> <div class="container"><form class="form m-3"> <div class="form-group"><input class="form-control" id="purchase_number" type="text" placeholder="PurchaseId*" /></div> <div><input id="btnSubmit" type="button" value="Submit" onclick="validatePurchaseID()" data-nl-bindto="action" data-nl-action="next" data-nl-transition="" /></div> </form></div> </div> </body> &lt;script&gt; function validatePurchaseID() { var purchase_number = ("#purchase_number").val(); document.controller.setValue('/ctx/vars/my_purchase_number', purchase_number); } &lt;/script&gt; </html>
jitendrak
Level 2
Level 2
Hi Manoj, This worked, there was some issue, i removed jquery links and that worked. appreciate your help