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

2
  • 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 Commented Jul 10, 2017 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! Commented Jul 10, 2017 at 18:52

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

16 Comments

Why you put consol.log? Is it necessary?
it is not, you can remove it.
Ok thanks, I'll check if it works in my full code and probably mark your answer as the best one
I found out that it changes action on elements[i].action = function() {myFunction()}; but it is no good because it sees it as a page URL, but onsubmit stays the same on elements[i].onsubmit = function() {myFunction()}; Anyways I'll try to figure out how to use action for this, probably I'll need javascript: before my code
I found out the solution elements[i].action = 'javascript:myFunction();'; should I put solution somewhere in this question? Because I'm new member on stack
|
1

Typo:

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

Comments

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/

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.