-1

my textarea and all its atributes is correct, but my javascript not right cannot set value of oTextbox3.

<html>
  <head>
    <title>Retrieving a Textbox Value Example</title>
  </head>
  <body> 
    <textarea rows="5" cols="25" name="txt2"></textarea> <br /> <textarea rows="5"  cols="25" name="txt3"></textarea>
    <br />
    <input type="button" value="Set Values" onclick="setValues()" />

    <script type="text/javascript">
      function setValues() { 
      var oTextbox2= document.getElementById("txt2");
      oTextbox2 = oTextbox2.value; oTextbox2 = oTextbox2.split(" ");
      oTextbox2 = oTextbox2.sort();

      var oTextbox3 = document.getElementById("txt3"); 
      oTextbox3.value = oTextbox2;
      } 
    </script>

  </body>
</html>
4
  • a good practice is to not change the type of a variable so if you declared (and even named) a var to be oTextBox2 you shouldn't change it to its value and than an array of words. Commented Nov 3, 2013 at 9:50
  • also you can do all this in a single line: oTextbox2.value.split(" ").sort(); Commented Nov 3, 2013 at 9:51
  • thanks for input, i just started javascript and i see that simplicity is the key when coding Commented Nov 3, 2013 at 9:55
  • This question is similar to: Difference between id and name attributes in HTML. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Jan 5 at 7:38

3 Answers 3

2

You use document.getElementById but assign names to your DOM elements. Use id attributes instead:

<textarea rows="5" cols="25" id="txt2"></textarea> <br /> <textarea rows="5"  cols="25" id="txt3"></textarea>
Sign up to request clarification or add additional context in comments.

Comments

1

document.getElementById("txt3") will get the element by "ID", you used name="txt3" you should have use id="txt3" in your textarea

Comments

0

Did you mean oTextbox3.value = oTextbox2.value?

And if you're using getElementById, then you need to use id, not name.

EDIT: Or use document.getElementsByName:

var oTextbox2 = document.getElementsByName('txt2')[0];

3 Comments

it is easy to set value of textarea has an id atribute, but i dont know how to retrieve value of textarea if it only has a name attribute.
my final code with your help, works perfect. thank you <body> <textarea rows="5" cols="25" name="txt2"></textarea> <br /> <textarea rows="5" cols="25" name="txt3"></textarea> <br /> <input type="button" value="Set Values" onclick="setValues()" /> <script type="text/javascript"> function setValues() { var oTextbox2= document.getElementsByName("txt2")[0]; oTextbox2 = oTextbox2.value; oTextbox2 = oTextbox2.split(" "); oTextbox2 = oTextbox2.sort(); var oTextbox3 = document.getElementsByName("txt3")[0]; oTextbox3.value = oTextbox2; } </script> </body>
the idea is to sort everything in alphabetical order, when i use id, it works perfect, but when i use the name attribute it does not work

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.