2

I am not sure if this is possible, but I am getting a variable after some text input from the user. This variable is obtained in Javascript, but I need it later in the HTML body tag of my code. Is there a way to do this?

This is the Javascript code:

<script type="text/javascript">
 window.alert("Documento ha sido adjuntado!")
 <!--
 var name = prompt("Referencia del Documento: ", "");
 //-->
 window.close();
 </script>
2
  • Yes, but I can't work out what it is that you want to do. Can you provide a step-by-step process that explains what you want? Do you want JavaScript to insert the variable somewhere in the <body> tag, or elsewhere into an element/node within the body element? Commented Dec 6, 2011 at 16:55
  • David, I need the string coming from the javascript to be inserted in mysql database using servlets. I need something that I can just declare a String="something" and that "something" is the value from Javascript. Commented Dec 6, 2011 at 17:07

2 Answers 2

4

You could include a hidden element within your page and set the value you need within the hidden value, then when you need to reuse it, just reference the hidden field.

Working Example (Uses a textbox as opposed to a hidden field for viewing purposes)

HTML:

<input type="hidden" id="nameValue" />

Javascript:

 <script type="text/javascript">

 window.alert("Documento ha sido adjuntado!")

 var name = prompt("Referencia del Documento: ", "");

 //Sets the hidden value with "name"
 document.getElementById('nameValue').value= name;

 window.close();
 </script>

As per your specific question:

 <script type="text/javascript">

 window.alert("Documento ha sido adjuntado!")
 var name = prompt("Referencia del Documento: ", "");
 window.close();
 UpdateDatabase(name);

 function UpdateDatabase(yourString)
 {
     //Servlet call to Update Database with "yourString"
 }

 </script>
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks Rionmonster, but I need to use the string straightaway, the option you are giving me is by using a form within HTML... let´s say I want to use the string to insert it into a table in mySQL database... how can I use this value? is there a way to just say: String=namevalue;
If you include the hidden value within the form, after you set the value in the prompt, it will be set within the form. So when you submit the form, it should have the value that you set with your prompt.
That is clear, I understand that, the issue is that I don´t want to use the form. Basically I want the javascript prompt to allow the user to enter a document´s reference number, but on the same code I have a jsp tag that will insert the value into mysql. If I use the form I need to get another code to handle this. Do you see what I am trying to do?
I believe I understand, so you have a separate function that you need to insert the value into the DB? If that is the case, you can access the "Document Reference Number" that is stored in the hidden field via: document.getElementById('nameValue'). So for your DB call, you could set the following: String = document.getElementById('nameValue'). I'll try to add an example
1

Try using JavaScript to update the HTML element after you prompt for the value.

 var name = prompt();
 document.getElementById("MyElement").value = name;



<input type='text' id="MyElement" />

1 Comment

Thanks P. Campbell, but I need to use the string straightaway, the option you are giving me is by using a form within HTML... let´s say I want to use the string to insert it into a table in mySQL database... how can I use this value? is there a way to just say: String=namevalue;

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.