-1

Hi I am using code below, but getting 'undefined' as a result. How to fix this? I am not sure how to call for a class within a class.

function myFunction() {    
    document.getElementById("mydiv").innerHTML = document.getElementsByClassName("inline-keyword-marker").innerHTML; 
}
<span class="inline-keyword-marker valid">Product</span>

<p>Click the button to change the text in "mydiv".</p>

<button onclick="myFunction()">Try it</button>

<p id="mydiv">***</p>
Ivar
  • 6,138
  • 12
  • 49
  • 61
  • 4
    `document.getElementsByClassName("inline-keyword-marker")[0]`. Get elementS! – deEr. May 18 '18 at 10:38
  • 2
    Possible duplicate of [What do querySelectorAll, getElementsByClassName and other getElementsBy\* methods return?](https://stackoverflow.com/questions/10693845/what-do-queryselectorall-getelementsbyclassname-and-other-getelementsby-method) – Sebastian Simon May 18 '18 at 10:44

2 Answers2

0

Note: As getElementS states, getElementsByClassName returns an array like HTMLCollection.

So inorder to access the first element (your class inline-keyword-marker), you need to add [0] to your method like this:

document.getElementsByClassName('inline-keyword-marker')[0]

function myFunction() {
  document.getElementById("mydiv").innerHTML = document.getElementsByClassName("inline-keyword-marker")[0].innerHTML;
}
<span class="inline-keyword-marker valid">Product</span>

<p>Click the button to change the text in "mydiv".</p>

<button onclick="myFunction()">Try it</button>

<p id="mydiv">***</p>

You might also to read this. It explains your issue more detailed than I did.


To handle all your classes, you can use this method:

function myFunction() {
  var classes = document.getElementsByClassName('inline-keyword-marker'); // Get all elements
  var myDiv = document.getElementById('mydiv'); // Get the div for the result

  myDiv.innerHTML = ""; // At first, we clear the result div; not neccessary, just for the optic
  
  // Because we got a HTMLCollection back, we need to loop through it
  for (var i = 0; i < classes.length; i++) {
    myDiv.innerHTML += classes[i].innerHTML + "<br>"; // With [i] we access every element -> at first the first one, then the second on and so on
    // I also added a <br> tag, so the results get append beneath each other, not next to each other
  }
}
<span class="inline-keyword-marker valid">Product1</span>
<span class="inline-keyword-marker valid">Product2</span>
<span class="inline-keyword-marker valid">Product3</span>
<span class="inline-keyword-marker valid">Product4</span>
<span class="inline-keyword-marker valid">Product5</span>

<p>Click the button to change the text in "mydiv".</p>

<button onclick="myFunction()">Try it</button>

<p id="mydiv">***</p>
CodeF0x
  • 2,624
  • 6
  • 17
  • 28
0

Problem :

document.getElementsByClassName returns an array and not a single value so in order to select one you can do this [i] where is the index of the elements so :

document.getElementsByClassName("inline-keyword-marker")

becomes :

document.getElementsByClassName("inline-keyword-marker")[0]

and selects the first element with class inline-keyword-marker

So total JavaScript becomes :

myFunction = () => {
  document.getElementById("mydiv").innerHTML = document.getElementsByClassName("inline-keyword-marker")[0].innerHTML; 
}

Shorter :

myFunction = () => (d=document).getElementById("mydiv").innerHTML = d.getElementsByClassName("inline-keyword-marker")[0].innerHTML
<span class="inline-keyword-marker valid">Product</span>

<p>Click the button to change the text in "mydiv".</p>

<button onclick="myFunction()">Try it</button>

<p id="mydiv">***</p>
Muhammad Salman
  • 433
  • 6
  • 18