0

I'm having this little bump in a function, normally if the variable "receivedRDB" is undefined in the DOM, it is dynamically created in a form and if it is defined it takes the value of another variable "receivedR". But "receivedRDB" keeps being created, even when it is already defined in the DOM.

var receivedRDB = document.getElementsByName('receivedR')[0];
    if (typeof receivedRDB !== "undefined") {

        receivedR = JSON.parse(receivedRDB.value.split(",").slice(0));

    } else {

        receivedR = [];


    }
    if (typeof receivedRDB !== "undefined") { //never detected
        receivedR.push(toRemoveR);
        receivedRDB.value = JSON.stringify(receivedR).replace(/"\[\\|\\"]|\\"/g, "");
    } else { //problematic part
        event.preventDefault();
        receivedR.push(toRemoveR);
        var input = document.createElement("input");
        input.type = "hidden";
        input.name = "receivedR";
        input.value = JSON.stringify(receivedR);
        rForm.appendChild(input);
    }
7
  • 2
    never detected - so, it's always undefined? stands to reason, none of your code actually "defines" it ... you say "receivedRDB" keeps being created ... where? not in that code it doesn't Commented Aug 21, 2017 at 1:33
  • 1
    This might be relevant too I guess : var receivedRDB = document.getElementsByName('receivedR')[0]; Commented Aug 21, 2017 at 1:36
  • yeah, where is that line of code in relation to the code in the question? Commented Aug 21, 2017 at 1:37
  • and if something is dynamic as you state, then you'll need to show a bit more of your code so we can see how you are doing it wrong Commented Aug 21, 2017 at 1:38
  • 1
    yes, and you state something is dynamically created - if that statement is run before the element is created, then receivedRDB wont magically be updated, you'll need to set the value of receivedRDB once the element exists Commented Aug 21, 2017 at 1:39

2 Answers 2

2

Here's a solution based on the fact that getElementsByName is a "live" list

Anywhere in your code you can put

var receivedRDB = document.getElementsByName('receivedR');

then change your code to

if (receivedRDB.length !== 0) {
    receivedR.push(toRemoveR);
    receivedRDB[0].value = JSON.stringify(receivedR).replace(/"\[\\|\\"]|\\"/g, "");
} else {
    event.preventDefault();
    receivedR.push(toRemoveR);
    var input = document.createElement("input");
    input.type = "hidden";
    input.name = "receivedR";
    input.value = JSON.stringify(receivedR);
    rForm.appendChild(input);
}
Sign up to request clarification or add additional context in comments.

Comments

0

I guess <script> tag which uses receivedRDB appears earlier than the DOM (in <head> for example). If so, there is more than one way to solve.

  1. <script defer>

    add an attribute defer to the <script> tag will make the JavaScript code be run after DOM is loaded

  2. window.onload = function(){ /* ... */ }

    window.onload will be called after DOM is loaded.

  3. window.addEventListener('load', function(){ /* ... */ })

    More compatible one (more than one function can be called individually)


If not the problem please include the minimal code to reproduce the problem.

Comments

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.