0

My code doesn't change element values I'm working in Visual Studio 2017 with cordova inAppBrowser and I'm not sure why it doesn't work? My code:

var elements = document.getElementsByTagName('div');

for (var i = 0; i < elements.length; i++) { 
  elements[i].innerHTMl = "sometext";
}

I want my code to change each element. I appreciate your help

David Thomas
  • 249,100
  • 51
  • 377
  • 410
Djordje Vujicic
  • 414
  • 5
  • 20
  • It works fine now with divs, but I used the same thing but with forms and elements[i].onSubmit = 'function(){ alert(string) }'; and it doesn't work on submiting a form, I also checked code with alert and it stays unchanged the whole time. If you have any idea why it doesn't work I would be soo thankful to you – Djordje Vujicic Jul 10 '17 at 17:48
  • I've found out the soulution for my problem. onsubmit didn't work for me so I used elements[i].action = 'javascript:myFunction();'; to do javascript action instead of redirecting page. Thanks everyone! – Djordje Vujicic Jul 10 '17 at 18:52

3 Answers3

1

Typo:

elements[i].innerHTMl should read elements[i].innerHTML (uppercase L)

grateful
  • 1,128
  • 13
  • 25
1

You have a typo in your code, it's innerHTML not innerHTMl here is a working fiddle https://jsfiddle.net/j8nbdm6b/2/

Nicolas
  • 8,077
  • 4
  • 21
  • 51
0

You have a lowercase L in innerHTML...

Here's a working demo

    (function() {
    document.getElementById('btn').addEventListener('click', function(e) {
      e.preventDefault();
      getDivs();
    })
  })()

  function getDivs() {
    var elements = document.getElementsByTagName('div');
        console.log(elements);
    for (var i = 0; i < elements.length; i++) { 
        elements[i].innerHTML = "sometext" + i;
    }
  }

https://jsfiddle.net/h7cgmxmf/

Araymer
  • 1,315
  • 1
  • 10
  • 16