Why this isn't working ? What's causing error?

javascript

#1


Script1.js:16 Uncaught TypeError: Cannot set property 'innerHTML' of null(anonymous function) @ Script1.js:16


// JavaScript source code

function pattern1(item, linemax) {
    var resultPattern = "";
    
    for (var line = 1; line <= linemax; line = line + 1){
        for (var unit = 1; unit <= line; unit = unit + 1){
            resultPattern += item;
        }
        resultPattern += "<br>";
    }
    console.log(resultPattern);
    return resultPattern;
}

document.getElementById("testid").innerHTML = pattern1("#", 7);


#2

Uncaught TypeError: Cannot set property 'innerHTML' of null

It means that document.getElementById("testid") returns null. Did you have HTML tag with id = "testid"?

<div id="testid"></div>

<script>
function pattern1(item, linemax) {
    var resultPattern = "";
    
    for (var line = 1; line <= linemax; line++){
        for (var unit = 1; unit <= line; unit++){
            resultPattern += item;
        }
        resultPattern += "<br>";
    }
    console.log(resultPattern);
    return resultPattern;
}

document.getElementById("testid").innerHTML = pattern1("#", 7);
</script>

Result:
#
##
###
####
#####
######
#######

#3

Yes ofcourse I gave an HTML tag same as you did. I gave it to div and
id="testid".

HELP ME.


#4

May I ask what you're trying to do? or achieve to be accurate


#5

@rishabh_prajapati, how you included Scriot1.js file? Should be:

<body>
   <div id="testid"></div>
   ...
   <script src="path/to/Script1.js"></script>
</body>

Please, post your HTML markup and describe what you expect in result. Because your js code seems correct.


#6

*************************HTML CODE***************************





test

<script src="Script1.js" type="text/javascript"></script>

<div id="testid">Hello, here will be the pattern!</div>



********************************JScript Coe ******************************
// JavaScript source code

var pattern1 = function(item, linemax) {
var resultPattern = "";

for (var line = 1; line <= linemax; line = line + 1){
    for (var unit = 1; unit <= line; unit = unit + 1){
        resultPattern += item;
    }
    resultPattern += "\n";
}
console.log(resultPattern);
return resultPattern;

}
var patternDone = pattern1("#", 7);
document.getElementById("testid").innerHTML = patternDone;


I don't know why it reaults as 'null'.


#7

I don't know why it reaults as 'null'.

Because you included your script file before div element, but you need after (look to my previous post).

remove <script src="Script1.js"></script> (without type attribute) and paste after <div id="testid">Hello, here will be the pattern!</div>

Read this tutorial: JavaScript Where To


#8

Okay. It worked but output is somewhat different than that of console.
Anyway can't we include the javascrpit file in ?


#9

It worked but output is somewhat different than that of console

maybe, problem here:

resultPattern += "\n";

you need to use "<br>" instead "\n". please, post your results (from console and from page).

Anyway can't we include the javascrpit file in ?

What you mean? I didn't understand question. We can include js files inside <head> tag or before close tag </body> (as I said in my prev posts). second is a good practice.


#10

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.