# Help with: document.getElementByClassName is not a function ?

**URL:** <https://forum.kirupa.com/t/help-with-document-getelementbyclassname-is-not-a-function/640240>\
**Category:** Uncategorized\
**Created:** [August 19, 2019, 11:58pm UTC](https://forum.kirupa.com/t/help-with-document-getelementbyclassname-is-not-a-function/640240 "2019-08-19T23:58:24Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![vmars316](https://avatars.discourse-cdn.com/v4/letter/v/bcef8e/32.png) [@vmars316](https://forum.kirupa.com/u/vmars316)\
**Post date:** [August 19, 2019, 11:58pm UTC](https://forum.kirupa.com/t/help-with-document-getelementbyclassname-is-not-a-function/640240/1 "2019-08-19T23:58:24Z")

</div>

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

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

```auto
<!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

---

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [August 20, 2019, 3:44am UTC](https://forum.kirupa.com/t/help-with-document-getelementbyclassname-is-not-a-function/640240/2 "2019-08-20T03:44:17Z")

</div>

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 😛 This will help: [Finding Elements in the DOM using querySelector](https://www.kirupa.com/html5/finding_elements_dom_using_querySelector.htm)

🙂

---

<div class="post-metadata">

**Author:** ![vmars316](https://avatars.discourse-cdn.com/v4/letter/v/bcef8e/32.png) [@vmars316](https://forum.kirupa.com/u/vmars316)\
**Post date:** [August 20, 2019, 10:22pm UTC](https://forum.kirupa.com/t/help-with-document-getelementbyclassname-is-not-a-function/640240/3 "2019-08-20T22:22:50Z")

</div>

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

```auto
<!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

---

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [August 21, 2019, 4:30am UTC](https://forum.kirupa.com/t/help-with-document-getelementbyclassname-is-not-a-function/640240/4 "2019-08-21T04:30:37Z")

</div>

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:

 ![image](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/2X/0/056b5cdb9eb97409469c09ff66aa0a7d41670ab1.png)

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

 ![image](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/2X/3/3057a29b2b7639f884e4b6a411b5889005b01ae1.png)

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 🙂

Cheers,  
Kirupa 😛

---

<div class="post-metadata">

**Author:** ![vmars316](https://avatars.discourse-cdn.com/v4/letter/v/bcef8e/32.png) [@vmars316](https://forum.kirupa.com/u/vmars316)\
**Post date:** [August 21, 2019, 4:07pm UTC](https://forum.kirupa.com/t/help-with-document-getelementbyclassname-is-not-a-function/640240/5 "2019-08-21T16:07:39Z")

</div>

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

```auto
<!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](https://www.w3schools.com/code/tryit.asp?filename=G78QU97EMY1O)

---

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [August 22, 2019, 5:54am UTC](https://forum.kirupa.com/t/help-with-document-getelementbyclassname-is-not-a-function/640240/6 "2019-08-22T05:54:13Z")

</div>

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

 ![image](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/2X/0/07eae3095223265f368cb26b1e0fe2652b9f19a4.png)

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` 🙂

🐵

---

<div class="post-metadata">

**Author:** ![vmars316](https://avatars.discourse-cdn.com/v4/letter/v/bcef8e/32.png) [@vmars316](https://forum.kirupa.com/u/vmars316)\
**Post date:** [August 22, 2019, 5:42pm UTC](https://forum.kirupa.com/t/help-with-document-getelementbyclassname-is-not-a-function/640240/7 "2019-08-22T17:42:50Z")

</div>

Thank you so much !  
Here is the corrected code:

```auto
<!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>

```

---

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [August 22, 2019, 5:50pm UTC](https://forum.kirupa.com/t/help-with-document-getelementbyclassname-is-not-a-function/640240/8 "2019-08-22T17:50:47Z")

</div>

Glad you got it working 🙂

---

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [August 27, 2019, 8:29pm UTC](https://forum.kirupa.com/t/help-with-document-getelementbyclassname-is-not-a-function/640240/10 "2019-08-27T20:29:47Z")

</div>

Is there a question here, @Alexander_Thomas? 😛
