Remove empty tag with javascript
-
I need some help creating some javascript that will remove an empty img tag. Ok.. So i have the following code in my page. What i am looking to do, is add the ability to remove the empty img tag(<img src="">) by clicking on the Select All button. If you can show me how to code that, or point me in the right direction (keeping in mind that I have 0 experience with js), I would be greatly appreciative. Note: Having the script run by clicking on this button isn't really what i want, but the way I see it (which may be, and usually is wrong) seems like the only way to make sure it only runs on the one page i need it to run on. The site consists of many, many includes. The page I need the script on is structured like so... main.html (containing the HEAD and BODY tag) and then page_i_want_script_to_run.html (containing the code I want the script to run on). Is there a way to get the script to run on page_i_want_script_to_run.html automatically on page load, without putting it in the BODY tag of main.html? If so, I would probably go that route. <!-- Main.html --> <HEAD> <SCRIPT LANGUAGE="JavaScript"> <!-- Begin function copyit(theField) { var tempval=eval("document."+theField) tempval.focus() tempval.select() therange=tempval.createTextRange() therange.execCommand("Copy") } // End --> </script> </HEAD> <BODY> <!-- Main.html --> <!-- page_i_want_script_to_run.html --> <form name="it"> <div align="center"> <input onclick="copyit('it.select1')" type="button" value="Select All" name="cpy"> <textarea class="code" name="select1" rows="3" cols="25"> <img src="example.jpg"> <img src=""> <!-- help me remove this tag... --> <img src="example2.jpg"> </textarea> </div> </form> <!-- page_i_want_script_to_run.html -->
-
Answer:
This is really easy with http://jquery.com/: $(img[src=""]).remove();
B(oYo)BIES at Ask.Metafilter.Com Visit the source
Other answers
I reread your question a few times, but I'm not seeing it: Why can't you just edit the HTML? Perhaps that not an option, but whenever possible, I'm a "cure the disease, not the symptoms" type of guy.
niles
If you can surround each img tag with a div or span and then give each a unique id this may help. function removeElement(divNum) { var d = document.getElementById('myDiv'); var olddiv = document.getElementById(divNum); d.removeChild(olddiv); } http://www.dustindiaz.com/add-and-remove-html-elements-dynamically-with-javascript/
dyno04
The below JavaScript should remove all img tags from a page the src being blank. There's no need to wrap it around a div or span. var imgTags = document.getElementsByTagName("img"); for(var i = 0; i if(imgTags[i].getAttribute("src") === "") imgTags[i].parentNode.removeChild(imgTags[i]); } I'll admit that the jQuery version is much nicer but sometimes you don't want to use a library. If you don't know much about JavaScript, I'd recommend getting a book such as JavaScript: The Definitive Guide to get to grips with it, or at least using http://developer.mozilla.org/en/docs/.>
HaloMan
Sadly the site mangled my above code. I meant this: var imgTags = document.getElementsByTagName("img"); for(var i = 0; i < imgTags.length; i++) { if(imgTags[i].getAttribute("src") === "") imgTags[i].parentNode.removeChild(imgTags[i]); }
HaloMan
Is there a way to get the script to run on page_i_want_script_to_run.html automatically on page load, without putting it in the BODY tag of main.html? If so, I would probably go that route. If you put the code in a SCRIPT tag at the bottom of page_i_want_script_to_run.html, it will only run if that include is loaded and only when the other HTML code on that page has been read. If you want it to run just as if it had been included in the body ONLOAD attribute, you can extend that attribute within the script itself with window.onload = window.onload+ 'removeImg()' (this way you don't overwrite any other onload events you may have). To be cleaner, you should put your code in a .js file and refer to it in the SCRIPT tag via the SRC attribute so you can reuse it on other pages if required.
Sparx
Neither of the above solutions seem to be working. http://thelastreal.com/rmv_img.html See something I am doing wrong?
B(oYo)BIES
I think the jQuery example should be $('img[src=""]').remove();
JonB
Or for the javascript part you need to move the script below the image tag. Basically that script is running as the page loads and it doesn't know there is an img tag there yet. Put it at the end of the page to be sure you get all img tags on the page.
unsigned
That unfortunately did not work either. Thanks for giving it a shot.
B(oYo)BIES
Related Q & A:
- How do I remove a site from IIS7 using JavaScript?Best solution by Server Fault
- How can I remove duplicate Objects from array of Objects in javascript?Best solution by Stack Overflow
- How to remove empty space above the chart?Best solution by stackoverflow.com
- I cannot empty my outlook in the box, can I empty the inbox all at once.Best solution by Yahoo! Answers
- Why does the head tag is empty in firebug page source?Best solution by Stack Overflow
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.