Safari Javascript error- TypeError: null is not an object

Problem:
Applescript errors when executing a javascript in Safari.

Javascript:

document.getElementById(“ap1_header_lblPendingRxCount”).innerText

When executed in Safari’s javascript console, the javascript executes successfully and returns an integer as text, reflecting the count of pending rx.

When compiled in Applescript, the javascript looks like this.

Applescript:

tell application "Safari"
	set js to "document.getElementById(\"ap1_header_lblPendingRxCount\").innerText"
	set PendingRxCount to do JavaScript js in document 1
end tell

When executed in AppleScript, Safari’s Javascript console returns a “null is not an object” error:

Console error:

TypeError: null is not an object (evaluating ‘document.getElementById(“ap1_header_lblPendingRxCount”).innerText’)

Question:
How can I correct this Applescript to have it successfully execute this javascript?

I’m not a JS expert but as far as I can see, your original JS script differs from what you send in AppleScript:

you have another document.getElementById in the original script

Thank you leo_r, and I have since corrected the javascript.