/    Sign up×
Community /Pin to ProfileBookmark

Uncaught TypeError: document.getElementById(…) is null

I have no idea why I am getting this error and I can’t seem to fix it.
Could someone point out my errors?

My code:

“`

$UCIROW = “quantity” .$row[‘ID’];
<input
oninput=”UpdateCartItem(<?php echo $row[‘ID’]; ?>, <?php echo $UCIROW; ?>)”
type=”number” id=”<?php echo $UCIROW; ?>”
style=”width: 8%;”
value=”<?php echo $row[‘AMOUNT’]; ?>”
>

<script type=”text/javascript”>
function UpdateCartItem(CartID, UCIROW) {
var quantity = document.getElementById(UCIROW).value;
jQuery.ajax({
url: “cart_handlerz.php”,
data: ‘CartID=’+ CartID + ‘&quantity=’+ quantity,
type: “POST”,
success:function(data){
LoadNavbarLinks()
},
error:function (){}
});
}
</script>

“`

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumSep 19.2020 — The HTML und javascript being created is this (for testing I set ID to 124 and AMOUNT to 5):
&lt;input
oninput="UpdateCartItem(124, quantity124)"
type="number" id="quantity124"
style="width: 8%;"
value="5"
<i>&gt;</i>

&lt;script type="text/javascript"&gt;
function UpdateCartItem(CartID, UCIROW) {
var quantity = document.getElementById(UCIROW).value;
jQuery.ajax({
url: "cart_handlerz.php",
data: 'CartID='+ CartID + '&amp;quantity='+ quantity,
type: "POST",
success:function(data){
LoadNavbarLinks()
},
error:function (){}
});
}
&lt;/script&gt;
Note that in the second line there are no quotes around `quantity124</C>. Therefore it is interpreted as a variable, not a string. The ID of your input reads: "quantity124". Most browsers create a variable for a DOM element whose name is equal to the ID. Therefore the complete DOM element is handed over to the function and later on to getElementById, however this function expects a string.<br/>
Putting single quotes around $UCIROW should fix the error:<br/>
<C>
oninput="UpdateCartItem(&lt;?php echo $row['ID']; ?&gt;, '&lt;?php echo $UCIROW; ?&gt;')"`
Copy linkTweet thisAlerts:
@uwrpauthorSep 19.2020 — Thank you for explaining it!

It works like a charm now :)
Copy linkTweet thisAlerts:
@uwrpauthorSep 19.2020 — Do you have any idea what the cause of this error could be?

I have tried googling it but I'm getting non similar issues.

Uncaught SyntaxError: expected expression, got '<'

I'm not sure whether it's related to my javascript code or php code. I also can't imagine where it would require a <.

This is my code:

``<i>
</i>
function Checkout() {
var delay = 900;
$("#checkout-submit").hide();
$("#loaderIconx").show();
jQuery.ajax({
url: "cart_checkout.php",
success:function(data){
setTimeout(function() {
$("#checkout-submit").show();
$("#checkout-submit").html(data);
$("#loaderIconx").hide();
}, delay);
},
error:function (){}
});
}<i>
</i>
``
Copy linkTweet thisAlerts:
@SempervivumSep 19.2020 — My editor doesn't show up any syntax error. What's the line where the error occurs?
Copy linkTweet thisAlerts:
@paulocressNov 09.2021 — This error TypeError: [document.getelementbyid](http://net-informations.com/js/iq/error.htm)(...) is null would seem to indicate that there is no such element with an ID passed to getElementById() exist. This can happen if the JavaScript code is executed before the page is fully loaded, so its not able to find the element. The solution is that you need to put your JavaScript code after the closure of the HTML element or more generally before < /body > tag.
Copy linkTweet thisAlerts:
@NogDogNov 09.2021 — {"locked":true}
×

Success!

Help @uwrp spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...