سؤال

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

<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.getElementsByName("txt2")[0];   
      oTextbox2 = oTextbox2.value.split(" ").sort();

      var oTextbox3 = document.getElementsByName("txt3")[0]; 
      oTextbox3.value = oTextbox2;
    } 
    </script>
</body>
</html>
هل كانت مفيدة؟

المحلول

Change this:

  oTextbox2 = oTextbox2.value.split(" ").sort();

to:

  oTextbox2 = oTextbox2.value.split(" ").sort().join(" ");

You need to use .join() to turn the sorted array back into a string.

FIDDLE

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top