ChippeRockTheMurph ChippeRockTheMurph -3 years ago 137
CSS Question

Remove Element When Page is Viewed using Internet Explorer

I have the following bit of code shown below:

<div class="timer" id="timer"><img src="http://i.imgur.com/87XaOWA.png"><p class="close-message" id="close-message"></p></div>


Now, when the page is viewed in Internet Explorer I want the
div
to be removed.

Since IE doesn't support the .remove() function I have found the following solution to circumvent the problem here. I have also found the following fiddle that can detect which browser is being used to view the page.

I've tried the following two
if
statements to remove the
div
tag when viewed in IE to no avail:

// Internet Explorer 6-11
var isIE = /*@cc_on!@*/false || !!document.documentMode

if (isIE = false) {
jQuery("#timer").eq(i).remove();
}


and

// Internet Explorer 6-11
var isIE = /*@cc_on!@*/false || !!document.documentMode

if (isIE = false) {
var node = document.getElementsById('timer')[i];
node.parentNode.removeChild(node);
}


What am I doing wrong? Individually the two components work fine, but when I try to use them together they don't work.

Answer Source

Your first error is:

if (isIE = false) {

you need to use a double equal sign for comparing, and in your case it should be:

if (isIE == true) {

The second error is:

document.getElementsById('timer')[i]

change it to:

document.getElementById('timer')

var isIE = /*@cc_on!@*/false || !!document.documentMode;

if (isIE == true) {
    var node = document.getElementById('timer');
    node.parentNode.removeChild(node);
}

//
// in jQuery: remove: .eq(i)....
//
if (isIE == true) {
  jQuery("#timer").remove();
}
<div class="timer" id="timer"><img src="http://i.imgur.com/87XaOWA.png"><p class="close-message" id="close-message"></p></div>

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download