0

My code:

Array.from(document.querySelectorAll("[id^='MTG_INSTR$']") ).forEach( el => {
  el.textContent = "Test";
});

This would be pass by value and won't make the assignment from what I've read.

Array.from(document.querySelectorAll("[id^='MTG_INSTR$']") ).forEach( el => 
{

   arr[index].textContent = "test";
}); 

Should be a reference to each element and should work. But it's not. Is it because the array I create is unnamed, and thus I can't easily identify it?

My question - Why doesn't that second version work? It isn't making assignments on my chrome script, even though I know it's being executed. The page I'm on isn't displaying any changes, but every element beginning with MTG... should have its text content changed to "test".

It isn't. Why?

Edit:
Something like this:

Array.from(document.querySelectorAll("[id^='MTG_INSTR$']") )[0].textContent

Evaluated to the text, so I know the syntax is solid at least.

6
  • Not getting your question here Commented Jul 2, 2017 at 8:09
  • @Mr.Alien Posted the question explicitly. I'm just wondering why, in my script, that second code block isn't changing anything on the page. That edit I posted, that changes the text when I assign it something like ...[0].textContent = "tessst". (At least in console on the page) Commented Jul 2, 2017 at 8:12
  • By all standards, the first one should work fine... are you getting any errors? Commented Jul 2, 2017 at 8:12
  • @PatrickRoberts I'm a complete retard, it works fine. I intially forgot the .textContent part, but during my debugging ran into a huge post on SO talking about how it's a pass by value not reference so shouldn't commit the changes to the page, just to my new array. Jesus. Commented Jul 2, 2017 at 8:16
  • How your second code is even working? arr and index both should be undefined Commented Jul 2, 2017 at 8:17

1 Answer 1

1

This should work fine

Array.from(document.querySelectorAll("[id^='MTG_INSTR$']") ).forEach( el => {
  el.textContent = "Test";
});

Second one should be

Array.from(document.querySelectorAll("[id^='MTG_INSTR$']") ).forEach( (el,index,arr) => 
{

   arr[index].textContent = "test";
}); 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.