0

I know this has been asked so many times, but i couldn't find the answer for my scenerio. Below is my code. I'm getting this error in the last line inside findPattern function. But i have that ID created inside createMyArea as textArea += 'id="P'+app_page_id+'_VALUE'+key+'". But why this is not finding the ID? Please let me know what i'm doing wrong here. I'm loading this after the page loads

var app_page_id         = 40;
var app_pattern_page_id     = 32;
var app_id;
var checksum;
var lang;

$(document).ready(function() {
    app_id = $("#pFlowId").val();
    checksum = $("#pInstance").val();

    createMyFlow();

    function createMyFlow(){
        var result = new htmldb_Get(null, $v('pFlowId'), 'APPLICATION_PROCESS='+getAllLanguagesProc,  $v('pFlowStepId'));
        lang = jQuery.parseJSON(result.get()).lang;
        createMyArea();
        result = loadDataFromMyOra();
        if(result !== null)
            findPattern(result);
    }

    function createMyArea(){
        var textArea;
        for ( var key in lang){
            textArea = '<tr><td  align="right"><label for="P'+app_page_id+'_VALUE_'+key+'">';
            textArea += '<span class="optional">Name '+languages[key][0]+': </span></label></td>';
            textArea += '<td  align="left" valign="middle">';
            textArea += '<input type="hidden" name="p_arg_names" />';
            textArea += '<fieldset id="P'+app_page_id+'_VALUE_fieldset_'+key+'" class="textarea" tabindex="-1">';
            textArea += '<input name="p_t04" type="text" maxlength="50" size="32" value=""';
            textArea += 'id="P'+app_page_id+'_VALUE'+key+'" required="" class="text_field"></fieldset>'
            textArea += '</td></tr>';
        $('.formlayout').append(textArea);
        }
    }

    function findPattern(patterns)
    {
        var item = "";
        for ( var key in patterns){
            item = patterns[key]+"";
            item = item.replace(/&#44;/g,",");
            item = item.replace(/&#8220;/g,'"')
            document.getElementById("P"+app_page_id+"_VALUE"+key).value = item;   //error here
        }
    }
});

3 Answers 3

1

The error indicates document.getElementById("P"+app_page_id+"_VALUE"+key) is null. Work backwards and think of all the reasons it might be null:

  • What does "P"+app_page_id+"_VALUE"+key evaluate to? Does an element with this id exist in the DOM? (You can manually inspect the DOM tree with browser dev tools, or try running getElementById() in the dev console.)
  • An element with that id probably doesn't exist. You point out you are trying to create it, but is there any reason that createMyArea() might not be working?
    • If lang is an empty object, this loop will execute 0 times: for (var key in lang) ...
    • The for loop iterates on keys of the lang object, but inside the loop you access via languages[key]. That is a little weird.

Their are many more things to check, but this should give you an idea of things to look for. Work backwards and test all your assumptions!

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

Comments

0

Have you checked the generated HTML or is it impossible to provide such example?

Also I've noticed that you use var way more than you should for your variables. Have a quick read about when to use what here or here.

1 Comment

Not to be picky, but if you give such advice about var keyword, you should give more details. The OP didn't add ES6 tag to the question and might as well want to support ES5. If not precised, you can't tell and the advice could be wrong.
0

The function that generates the code createMyArea() uses the keys in the lang variable

for ( var key in lang){
....
textArea += 'id="P'+app_page_id+'_VALUE'+key+'" required="" class="text_field"></fieldset>'
....

but in the findPatterns you are checking the id of the document using keys from another variable

for ( var key in patterns){
....
document.getElementById("P"+app_page_id+"_VALUE"+key).value = item; 
....

If the patterns variable and lang variable don't have the same key, then you will get an error.

To check that, you can

console.log ("P'+app_page_id+'_VALUE'+key+'")

just before the document.getElementById and see what values is checked there.

1 Comment

I checked. Both are same values

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.