Nischay
Nischay

Reputation: 13

Javascript or HTML DOM function required to hold values of textarea for my chrome extension

My code is :

var menuItems = document.getElementsByTagName('textarea');
for (var menuItemIndex = 0 ; menuItems.length ; menuItemIndex ++){
    var currentMenuItem = menuItems[menuItemIndex];
    currentMenuItem.value = "Items changed:\n\nDescription of change:\n\nDescription of test:\n";
}

But when I am clicking to the event "Attach" the values in currentMenuItem.value are coming but the value which I am filling manually in front of Items changed, Description of Change, Description of Test i.e ABC, XYZ, FGH are not coming.

screenshot of output in gui

Upvotes: 1

Views: 55

Answers (3)

EugenSunic
EugenSunic

Reputation: 13703

You could use regex once you obtain the value of the entire textArea.

Extract the values for all three rows, after inserting the text click on the attach button and you it should console.log the output string.

Insert the following text in the textarea for testing:

Items changed: ABC

Description of change: EFG

Description of test: HIJ

function getTxtAreaValues() {
  const txtAreaContent = document.getElementsByTagName('textarea')[0].value

  const regexExpresion1 = /Items changed:(.*)/g;
  const regexExpresion2 = /Description of change:(.*)/g;
  const regexExpresion3 = /Description of test:(.*)/g;

  const match1 = regexExpresion1.exec(txtAreaContent)[1];
  const match2 = regexExpresion2.exec(txtAreaContent)[1];
  const match3 = regexExpresion3.exec(txtAreaContent)[1];


  const outputString =
    `Items changed:${match1}\n
     Description of change:${match2}\n 
     Description of test:${match3}\n 
`;
  return outputString;
}


document.getElementById('attach').addEventListener('click', () => {
  const values = getTxtAreaValues();
  console.log(values);
})

Upvotes: 0

Maestro
Maestro

Reputation: 865

You made an infinite loop with your current code!

Your for instruction should look like this :

for (var menuItemIndex = 0 ; menuItemIndex < menuItems.length ; menuItemIndex ++){

Documentation

Upvotes: 1

jonasdev
jonasdev

Reputation: 736

I think your for loop is meant to look like this:

for (var menuItemIndex = 0 ; menuItemIndex < menuItems.length ; menuItemIndex ++){

With your current implementation, it will be an infinite loop.

Upvotes: 0

Related Questions