Backticks and class elemnt comby not working

Joined
Jan 6, 2022
Messages
8
Reaction score
0
Working on a snippet that just doesn't work. Backticks used to write html textareas while in the JS script.
May sound weird but I would really like to know where I've gone wrong after hours of work. Alert should show the result.


Code:
<script>
    var item="red"; // (for testing)

function doit(){
    var elements=document.getElementsByClassName(item).value;
    var found=elements[0];
    
alert(found); // text in left box (red);
}
document.write(`<textarea class ="${item}">red</textarea>`);
document.write(`<textarea class ="${item}">blue</textarea>`);
document.write('<button onclick ="doit()">CLICK</button>');
</script>
 
Joined
Jul 4, 2023
Messages
453
Reaction score
54
In your case first you need a get reference to an element with class name "red" and then take the value.
HTML:
<script>
  var item = "red"; // (for testing)

  function doit() {
    var elements = document.getElementsByClassName(item);
    var found = elements[0].value;

    alert(found); // text in left box (red);
  }

  document.write(`<textarea class="${item}">red</textarea>`);
  document.write(`<textarea class="${item}">blue</textarea>`);
  document.write('<button onclick="doit()">CLICK</button>');
</script>

or
HTML:
<script>
  var item = "red"; // (for testing)

  function doit() {
    var elements = document.getElementsByClassName(item);
    if (elements[0]) {
      var found = elements[0].value;
      alert(found); // text in left box (red);
    }
  }

  document.write(`<textarea class="${item}">red</textarea>`);
  document.write(`<textarea class="${item}">blue</textarea>`);
  document.write('<button onclick="doit()">CLICK</button>');
</script>

or
HTML:
<script>
  var item = "red"; // (for testing)

  function doit() {
    var found = document.getElementsByClassName(item)[0].value.trim();
    if (found === item) {
      alert(found); // text in left box (red);
    }
  }

  document.write(`<textarea class="${item}">red</textarea>`);
  document.write(`<textarea class="${item}">blue</textarea>`);
  document.write('<button onclick="doit()">CLICK</button>');
</script>

BTW,
HTML:
<script>
  const item = 'red'; // (for testing)

  function doit() {
    const found = document.querySelector(`.${item}`).value.trim();
    if (found === item) {
      alert(found); // text in left box (red);
    }
  }

  const body = `
    <textarea class="${item}">red</textarea>
    <textarea class="${item}">blue</textarea>
    <button onclick="doit()">CLICK</button>
  `;
  document.body.innerHTML = body;
</script>
 
Last edited:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,880
Messages
2,569,944
Members
46,246
Latest member
RosalieMar

Latest Threads

Top