6

I'm trying to remove css file from document.

ths should work.. ==>

    document.getElementsByTagName("link")[1].remove();  

but, not working. and when I add some testing code. ==>

    document.getElementsByTagName("link")[1].remove();
    console.log(document.getElementsByTagName("link")[1].remove());

it's working now.

what is the problem.. or what do I miss ?

0

3 Answers 3

11

Remove is not a DOM node method. Maybe you confused it with the jQuery method?

Either use plain JavaScript:

var linkNode = document.getElementsByTagName('link')[1];
linkNode.parentNode.removeChild(linkNode);

Or jQuery:

$('link').eq(1).remove();
Sign up to request clarification or add additional context in comments.

3 Comments

it's good. still not explaining why it was working with console.log code ?
That is impossible, console.log does not delete DOM nodes, so there must be something else that deleted it. Maybe you pressed the delete or backspace button while the element was selected?
only thing I changed was the code.. anyhow it's not an issue here.. so. thanks.
5

much clear selection

use querySelector instead of getElementsByTagName

var linkNode = document.querySelector('link[href*="whatever.css"]');

1 Comment

very useful to remove any link added at runtime, identifying it by filename
-1

i would suggest jquery .removeClass() function. this can remove one or more classes assigned to any specified element or tag.

see documentation here http://api.jquery.com/removeclass/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.