0
What’s wrong with my code. I’ve tried it on Codepen and it works normally. Even the order of the elements are the same there, but in my Wordpress does not want to work. Note that here also works. Error displayed -> Typeerror: Cannot set Property 'innerHTML' of null
<label for="file">Envie sua receita.</label>
<input type="file" id="file" name="file" multiple>
<div id="div1"></div>
<a href="javascript:addCampo();"><i id="addFile" class="glyphicon glyphicon-plus">+</i></a>
<script>
var i = 1;
function addCampo(){
var idDiv = 'div'+i;
document.getElementById(idDiv).innerHTML = '<input type="file" id="file'+i+'" name="file'+i+'" multiple>';
i++;
}
</script>
It’s just like that. I only made one Ctrl+C & Ctrl+V.
This means that the element with its ID is not found, so the
getElementById
returns null and consequently the methodinnerHTML
does not exist. Make aconsole.log(idDiv);
right after variable declarationidDiv
and see if the elements exist in HTML with that ID.– Filipe Moraes
Do you want to create a div for each input? Type:
<div id="div1"><input></div>
,<div id="div2"><input></div>
,<div id="div3"><input></div>
etc.– Sam
So... in the
console.log(idDiv);
shows the stringdiv1
. That’s exactly what I want. I don’t understand what you mean.C:\Users\genes\javascript
λ node var.js
<br>div1
– Mr Genesis
Check this image: http://prntscr.com/inmugk
– Mr Genesis
You want to insert each input file created within the div id="div1"?
– Sam
The question is not where it will be placed, but why it shows the error only within Wordpress. Your doubt is in another part of the code that I haven’t even created yet. I think it’s related to what the
Paulo R. F. Amorim
answered. But that’s another thing. Note that in the question you have a code; run it! Note that you will add a new field by clicking on the "+". THAT DOESN’T HAPPEN IN MY TEST ENVIRONMENT.– Mr Genesis