0

I'm creating a chrome extension taht access the DOM of page and get value of some inputs . The problem is in the page that i want to access the value of an input text in my content_script i get :

Cannot read property 'val' of null

because , the inputs are generated dynamically from a js file that i cann't modify .

i put the : "run_at" : "document_end" in my manifest file and in the content script i wait the page to be fully loaded

 window.addEventListener("load", function load(event){
    window.removeEventListener("load", load, false); //remove listener, no longer needed

     // $("input[name$='incident.state']").val();

    var element =$('#incident.state').val();
    alert(element);


},false);
Lizinh
  • 71
  • 1
  • 1
  • 11
  • Use [the debugger](https://developer.chrome.com/devtools/docs/tips-and-tricks): set a breakpoint on that line and examine the page source in Elements panel. Also make sure you inject jQuery before your content script. – wOxxOm Aug 10 '16 at 17:59
  • i already added the jquery to my content scripts on the begening – Lizinh Aug 10 '16 at 20:21

1 Answers1

1

Since you have mentioned that the inputs are generated dynamically, it may still not exist when onload event fires. If you don't want to use something like setTimeout to defer retrieving the element, you could use MutationObserver to react to changes in the DOM, for example, observing the input being inserted.

Community
  • 1
  • 1
Haibara Ai
  • 10,703
  • 2
  • 31
  • 47