Javascript: document.getElementById() returns NULL

浪尽此生 提交于 2019-11-30 11:57:53

You never checked getElementById(...) for NULL.

You checked getElementById(...).value for NULL, and divs don't have a "value".

Also note that you forgot to close that <div /> tag, which is illegal in your XHTML... and used an SVG doctype for some reason. SVG is not HTML.

It's not really clear what you're trying to do here.

Sailab Rahi

The page contents need to be loaded before trying to read them. Try

window.onload = function() {
  // run your script in here
}

Or if you're using jQuery, prefer

$(document).ready(function() {
  ...
}

The "arect" element is a <div>, and <div> elements don't have a "value".

Get rid of that bogus SVG doctype too.

if(document.getElementById("arect").value == null){
    alert('NULL >> '+ xx.value);
  }

This code always returns null or error. If you want to see if the object exists, do the following....

if(xx == null)
   alert('Object does not exist');
else 
   alert('Object exists. Inner HTML: ' + xx.innerHTML);

Also, div does not have value. If you want to get the html inside div, use xx.innerHTML

Firstly, what you're trying to do is fraught with cross-browser inconsistency that would tax a javascript pro, so you will be better off using jQuery if you're new to javascript.

Secondly, xx will not have a value as it is a DIV. You will find that xx is not null in itself.

in my case it was because of having this line at the beginning of the jsp/html(whatever) file:

<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">

removing it solved my problem.

if the button is set to visisble= false then you cannot get the id of that button on client side. To hide the button use

button1.Style.Add("display","none")-- for visible false

and

button1.Style.Add("display","block")-- for visible true

and even if button is enabled false we cannot get the Id of the button on client side

You can get the id of the button by document.getElementById('<%= button1.ClientID %>'); Or if you set the ClientIDMode="Static" for the control in aspx page you can get it directly by document.getElementById('button1'); Or document.getElementById('MainContent_button1');--- MainContent here is the Id of the contentplaceholder if you have the id of the contenet placeholder different use that id_button1.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!