Help with: document.getElementByClassName is not a function ?

Hello & Thanks ,
Pls , what can use instead of:

  copyFromVar = document.getElementByClassName("copyFrom"[0]).value;
  document.getElementByClassName("pastInto"[0]).innerHtml   = "copyFromVar"
<!DOCTYPE html>
<html>
<body>
<h5>Copy-Text-from-textarea-to-div.html</h5>
Field1: <input type="text" id="field1" value="Hello from Field1 !"><br>
Field2: <input type="text" id="field2"><br><br>

<button onclick="myFunction()">Copy text From field1 to field2</button>
<button onclick="saveItAll()">Copy text From textarea to contentEditable-div</button>

<br><br>
<textarea  rows="4" cols="20" class="copyFrom" >TextArea1 </textarea>
<br>
<textarea  rows="4" cols="20" class="copyFrom" >TextArea2 </textarea>
<br>
<div class="pastInto" contentEditable="true" >pastInto 1</div>
<div class="pastInto" contentEditable="true" >pastInto 2</div>

<p>A function is triggered when the button is clicked. The function copies the text from Field1 into Field2.</p>
<script>
var copyFromVar; var pasteIntoVar;
var fromCount; //  = document.getElementsByClassName("copyFrom");
var intoCount;  //  = document.getElementsByClassName("pasteInto");

function saveItAll() {

  copyFromVar = document.getElementByClassName("copyFrom"[0]).value;
  document.getElementByClassName("pastInto"[0]).innerHtml   = "copyFromVar"

  copyFromVar = document.getElementByClassName("copyFrom"[1]).value;
  document.getElementByClassName("pastInto"[1]).innerHtml   = "copyFromVar"
  
}
</script>
<script>
function myFunction() {
  document.getElementById("field2").value = document.getElementById("field1").value;
}
</script>

</body>
</html>

Thanks

The getElementByClassName function takes a class value as its argument:

<p class="bar">Hello!</p>

To access this element, you would use document.getElementByClassName("bar"). I am not quite sure what you are trying to do here, but you can’t use the array syntax or anything non-class related inside the getElementByClassName function. Also, this function only returns one single element. If you want to access all elements by a class value and then target a particular element, you can use getElementsByClassName (note the plural form of elements) and then use the index position of the element you wish to access:

 var secondElement = document.getElementsByClassName("foo")[1];

If you want to use something more modern, you should look into querySelector and querySelectorAll. They are more flexible in the elements you can target beyond just class or id :stuck_out_tongue: This will help: Finding Elements in the DOM using querySelector

:slight_smile:

I am trying to copy from textarea 1
into div 1 .
Here is the revised code:

<!DOCTYPE html>
<html>
<body>
<h5>Copy-Text-from-textarea-to-div.html</h5>
Field1: <input type="text" id="field1" value="Hello from Field1 !"><br>
Field2: <input type="text" id="field2"><br><br>

<button onclick="myFunction()">Copy text From field1 to field2</button>
<button onclick="saveItAll()">Copy text From textarea to contentEditable-div</button>

<br><br>
<textarea  class="copyFrom"  rows="4" cols="20">TextArea1 </textarea>
<br>
<textarea class="copyFrom" rows="4" cols="20"  >TextArea2 </textarea>
<br>
<div class="pastInto" contentEditable="true" >pastInto 1</div>
<div class="pastInto" contentEditable="true" >pastInto 2</div>

<p>A function is triggered when the button is clicked. The function copies the text from Field1 into Field2.</p>
<script>
var copyFromVar = ""; var pasteIntoVar = "";
var fromCount = 0; //  = document.getElementsByClassName("copyFrom");
var intoCount = 0;  //  = document.getElementsByClassName("pasteInto");

function saveItAll() {
  fromCount   = document.getElementsByClassName("copyFrom");
  intoCount    = document.getElementsByClassName("pasteInto");
alert("fromCount" + fromCount + "    intoCount" + intoCount );

  copyFromVar = fromCount[0].innerHtml ;
  pasteInto[0].innerHtml   = copyFromVar;
  
alert("copyFromVar = " + copyFromVar + "    pasteIntoVar = " + pasteIntoVar );

  copyFromVar = fromCount[1].innerHtml ;
  pasteInto[1].innerHtml   = copyFromVar;
  
alert("copyFromVar = " + copyFromVar + "    pasteIntoVar = " + pasteIntoVar );
  
}
</script>
<script>
function myFunction() {
  document.getElementById("field2").value = document.getElementById("field1").value;
}
</script>

</body>
</html>

Pls , show me my coding errors .
Thanks

When you run into something that doesn’t work, always check your Console to see if any errors are raised. In your case, copying into the div shows this:

You misspelled pasteInto in your HTML. After fixing that, your next error is this:

In your code, you never declared a variable called pasteInto. My guess is that you want that variable to store the results of a getElementsByClassName, but I’ll leave it to you to investigate further given these pointers :slight_smile:

Cheers,
Kirupa :stuck_out_tongue:

Thanks:
Ah finally , code is error free ,
but no innerHtml is changed .
What am I doing wrong ?
Thanks:

<!DOCTYPE html>
<html>
<body>
<h5>Copy-Text-from-textarea-to-div.html</h5>
Field1: <input type="text" id="field1" value="Hello from Field1 !"><br>
Field2: <input type="text" id="field2"><br><br>

<button onclick="myFunction()">Copy text From field1 to field2</button>
<button onclick="saveItAll()">Copy text From textarea to contentEditable-div</button>

<br><br>
<textarea  class="copyFrom"  rows="4" cols="20">TextArea1 </textarea>
<br>
<textarea class="copyFrom" rows="4" cols="20"  >TextArea2 </textarea>
<br>
<div class="pasteInto" contentEditable="true" >pasteInto 1</div>
<div class="pasteInto" contentEditable="true" >pasteInto 2</div>

<p>A function is triggered when the button is clicked. The function copies the text from Field1 into Field2.</p>
<script>
var copyFromVar = ""; 
var fromCount = 0; //  = document.getElementsByClassName("copyFrom");
var intoCount = 0;  //  = document.getElementsByClassName("pasteInto");
function saveItAll() {
  fromCount   = document.getElementsByClassName("copyFrom");
  intoCount    = document.getElementsByClassName("pasteInto");

  copyFromVar = fromCount[0].innerHtml ;
  intoCount[0].innerHtml   = copyFromVar;
alert("1 copyFromVar = " + copyFromVar );
  
  copyFromVar = fromCount[1].innerHtml ;
  intoCount[1].innerHtml   = copyFromVar;
alert("2 copyFromVar = " + copyFromVar );
  
}
</script>
<script>
function myFunction() {
  document.getElementById("field2").value = document.getElementById("field1").value;
}
</script>

</body>
</html>

And for 30 days here:
https://www.w3schools.com/code/tryit.asp?filename=G78QU97EMY1O

You have an alert statement that seems to indicate a problem:

Did you investigate what may be causing this?

Some hints: The way to get the value of an input element or textarea is not through innerHTML, but through value. Also, there is no valid property called innerHtml. It should be innerHTML :slight_smile:

:monkey_face:

Thank you so much !
Here is the corrected code:

<!DOCTYPE html>
<html>
<body>
<h5>Copy-Text-from-textarea-to-div.html</h5>
Field1: <input type="text" id="field1" value="Hello from Field1 !"><br>
Field2: <input type="text" id="field2"><br><br>

<button onclick="myFunction()">Copy text From field1 to field2</button>
<button onclick="saveItAll()">Copy text From textarea to contentEditable-div</button>

<br><br>
<textarea  class="copyFrom"  rows="4" cols="20">TextArea1 </textarea>
<br>
<textarea class="copyFrom" rows="4" cols="20"  >TextArea2 </textarea>
<br>
<div class="pasteInto" contentEditable="false" >pasteInto 1</div>
<div class="pasteInto" contentEditable="true" >pasteInto 2</div>

<p>A function is triggered when the button is clicked. The function copies the text from Field1 into Field2.</p>
<script>
var copyFromVar = ""; 
var fromList; //  = document.getElementsByClassName("copyFrom");
var intoList;  //  = document.getElementsByClassName("pasteInto");
function saveItAll() {
  fromList   = document.getElementsByClassName("copyFrom");
  intoList    = document.getElementsByClassName("pasteInto");

  copyFromVar = fromList[0].value ;
  intoList[0].innerHTML   = copyFromVar;
//alert("0 copyFromVar = " + copyFromVar );
  
  copyFromVar = fromList[1].value ;
  intoList[1].innerHTML   = copyFromVar;
//alert("1 copyFromVar = " + copyFromVar );
  
}
</script>
<script>
function myFunction() {
  document.getElementById("field2").value = document.getElementById("field1").value;
}
</script>

</body>
</html>
1 Like

Glad you got it working :slight_smile:

Is there a question here, @Alexander_Thomas? :stuck_out_tongue: