3

I have done this and it doesn't seem to be working!:

Javascript:

<script>
function hideOptionPhoto(){ 
    var element = document.getElementById("Photo1");
    element.parentNode.removeChild(Photo);   
};

window.onload = function() {
    hideOptionPhoto();
};
</script>

HTML:

<div id=Photo1>
    <input id="Photo" type="image" src="x.png" border="0" name="submit" alt="">
</div>

I put the <input> inside a <div> because of the parent and child situation. Is that correct?

2
  • Even if Photo reference the input element, Photo is a child of Photo1, not of the parent of Photo1. Hence the element.parentNode.removeChild(Photo); won't work. Learn how to debug JavaScript. Commented Aug 10, 2013 at 17:15
  • @maxmitch Your HTML has a Syntax error in the first Line. CHeck my answer. Explanation included Commented Aug 10, 2013 at 17:59

5 Answers 5

10

Try this out. This will work

The below script should be put inside the body tag

<script>
function hideOptionPhoto(){


var element = document.getElementById("Photo1");
var child=document.getElementById("Photo");
element.removeChild(child);

}
window.onload = function() {
  hideOptionPhoto();
};
</script>
Sign up to request clarification or add additional context in comments.

Comments

6
var element = document.getElementById("Photo"); // notice the change
element.parentNode.removeChild(element);

The <div> is optional (for this) since every element has a parentNode. But there might be other reasons for having the div.

4 Comments

plz see working jsfiddle. or see this stackoverflow.com/questions/3387427/…
Sorry I was using the live function on dreamweaver and doesn't seem to be working. Just putting it on the internet now to see if it works!
This is a very stupid question and irrelevant but is there an option other than .onload that does it earlier? And it doesn't seem to be working... Weird, as it is obviously working on jsfiddle! I have put it inside an if statement but I assume that won't make a difference?
You put put a small script immediately after the element. It can't execute earlier than that, or the element won't exist, and you won't be able to get a reference to it.
1

Ok. Let me post the working fiddle and the I will give the explanation.

Working Fiddle

In your code there was a "Syntax Error".

//Incorrect    
     <div id=Photo1>

//Correct
    <div id="Photo1">

In addition check my JavaScript function. The function call was ok. Just the code inside it was wrong

You already assigned the HTMLelement div(Photo1) to the variable "Element". The img("photo") is a child of Element and hence can be directly removed.

One more important point is the naming conventions that you use. You should not assign ID's like "photo"

HTML

<div id="Photo1">
    <input id="Photo" type="image" src="x.png" border="0" name="submit" alt=""/>
</div>

Javascript

function hideOptionPhoto(){     
    var element = document.getElementById("Photo1");
    var child=document.getElementById("Photo");
    element.removeChild(child);
};

window.onload = function() {
    hideOptionPhoto();
};

2 Comments

The OP isn't using XHTML, omitting the quotes around an attribute value consisting entirely of letters is not an error of any kind.
You should not assign ID's like "photo" — What is wrong with "photo" as an ID?
0

Try

function hideOptionPhoto(){ 
var element =  document.getElementById('Photo');
if (typeof(element) != 'undefined' && element != null)
  {
    element.remove();
    alert('Deleted');
  }

};

window.onload = function() {
    hideOptionPhoto();
};
<div id=Photo1>
    <input id="Photo" type="image" src="x.png" border="0" name="submit" alt="">
</div>

Comments

-2
<div id="Photo1">
<input id="Photo" type="image" src="x.png" border="0" name="submit" alt="">
</div>

Use this:

document.getElementById("Photo1").innerHTML = "";

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.