开发者

How can I tell 'Whether a Javascript object exists on my page'?

开发者 https://www.devze.com 2023-03-12 01:52 出处:网络
I can\'t get the the conditional to work in this function, any help? function headupdate(id, name, heading)

I can't get the the conditional to work in this function, any help?

function headupdate(id, name, heading)
{
    var order;
    if (document.getElementById(heading).value === undefined)
    {
        order = 1;
    }
    else
    {
        order = document.getElementById(heading).value;
    }
    alert(order);
    $.post('headingupdate.php', {id: id, name: name, value: heading, order: order},
        function(response)
        开发者_Go百科{
            $('#resume').html(response)
        }
    )
};


You should check as following.

var head = document.getElementById(heading);
if(head!=null)
{
  order = head.value;
}
else
{
order=1;
}


In response to your post title, I use typeof() to find if an element exists in the DOM:

if(typeof(document.getElementById('someElement')=='undefined')) {
  alert('Element DNE');
}

Also, typeof() returns a string, so it needs to be in quotes for a conditional statement

"undefined"
0

精彩评论

暂无评论...
验证码 换一张
取 消