0

I am trying to get the values of a textbox dynamically using an ajax call. I tried to access the source and it shows the textbox exist but it give an error that the textbox value cannot be read

var status, classs; 
    if(document.getElementById(stud).checked == true){
        status = "Checked";
    }
    else{
        status = "Unchecked";
    }

    var classs = document.getElementById("class_id").value;
    var session = document.getElementById("session").value;
    var term = document.getElementById("term").value;

it gives the error message on all the last three lines of code saying the class_id, session and term does not exist. I actually created an hidden field to hold different values but yet it complained that it does not exist. Any help will be appreciated. Thanks

Teejaygenius
  • 29
  • 1
  • 1
  • 5
  • Share your HTML please. And check that your JS code is not executed before your fileds are available. – ADreNaLiNe-DJ Jul 10 '17 at 08:05
  • If an element with the ID exists as of when you call `getElementById`, `getElementById` **will** return a reference to the element. Thus, if you're getting back `null`, it means an element with that ID *doesn't* exist as of when you're making the call. Clearly, there is no element with `id="class_id"`, or no element with `id="session"`, or no element with `id="term"` (depending on which of those three returned `null`) when you run the code above. – T.J. Crowder Jul 10 '17 at 08:06
  • Side note: `checked` is a boolean. You don't need to compare booleans with `true`/`false` to get a boolean; you already have one. I mean, where do you stop? `if ((checked == true) == true)`? `if (((checked == true) == true) == true)`? :-) Just `if (checked)` is all you need. – T.J. Crowder Jul 10 '17 at 08:08

0 Answers0