1

Javascript:

function changeIt()
{
    var i = 1;
    var j = 1;
    my_div.innerHTML = "<br><br><input type='text' name='mytext' + i  value='pleaseEnter malzeme' >   <input type='text' name='mytet2'+j value='please quantity'> "
}

HTML:

<input type="button" value="Add Ingredients" onClick="changeIt()" />
<div id="my_div"></div>

I have these code piece to add textbox dynamically in my HTML file. Problem is that I cannot reach text.

System.out.print(request.getParameter("mytext1")); //doesnt work to reach mytext1 value. 

How can I solve this problem? Thanks in advance.

4
  • You don't seem to have a function request.getParameter nor do you have out.print. Have you tried looking in your console? (hit F12) you'll see a bunch of errors in there. Commented Apr 22, 2016 at 22:12
  • Is this a Java question or a JavaScript question? Either way, you aren't showing us enough code. Commented Apr 22, 2016 at 22:15
  • @MikeC That is Java code. They are showing the back-end, which has no F12. Commented Apr 22, 2016 at 22:19
  • out.print(request.getParameter("mytext1")) returns null value, but when i change to out.print(request.getParameter("mytext")) returns not null value. I think problem is on concatenation . Commented Apr 22, 2016 at 22:27

2 Answers 2

1

Okay, first to get a reference of an element in JavaScript you need to call document.getElementById("my_div"). There is no such thing as automatic global scope population with IDs in JS.

Then your HTML string contains variables which are written as part of the string. Concatenation in JS works with the plus sign (+). So your string should be:

"<br><br><input type='text' name='mytext" + i + "' value='pleaseEnter malzeme'><input type='text' name='mytet2" + j + "' value='please quantity'>"

Everything assembled together:

<script type="text/javascript">
function changeIt() {
    var i = 1;
    var j = 1;
    document.getElementById("my_div").innerHTML = "<br><br><input type='text' name='mytext" + i + "' value='pleaseEnter malzeme'><input type='text' name='mytet2" + j + "' value='please quantity'>"
}
</script>

<input type="button"  value="Add Ingredients" onClick="changeIt()">
<div id="my_div"></div>

Regarding the print I have no idea what you're actually trying there...

Sign up to request clarification or add additional context in comments.

3 Comments

really thanks for this answer. Nearly 5-6 hours, i tried to solve this problem.
There is no such thing as automatic global scope population with IDs in JS. See stackoverflow.com/questions/6381425/…
Interesting. Thanks for the info. However I think JS starters are better off not knowing something like this exists. ;-)
1

Try changing your innerHTML to this:

my_div.innerHTML ="<br><br><input type='text' name='mytext" + i + "' value='pleaseEnter malzeme' >   <input type='text' name='mytet2" +j + "' value='please quantity'> "

Comments

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.