HTML Question

I want to delete from innerHTML

I have innerHTML like this:

<img class="ytimg" src = "" >
<div class = "videotext" >
<h5> Bootstrap tutorial #1</h5>
<p>Lorem ipsum.</p>
<p>Lorem ipsum dolor.</p>

I want to delete every single word and tag from it and I want to keep only one thing.


My code now:


Answer Source

Can't see how you get the containing element whose innerHTML you're working with, so there's an assumption first up with the div id="something"

var blah = document.getElementById('something');
var img = blah.querySelector('#ytimg');
var split = img.src.split('/');
blah.innerHTML = split.pop();
<div id="something">
  <img class = 'ytimg' id='ytimg' src = "" >
  <div class = "videotext" >
    <h5> Bootstrap tutorial #1</h5>
    <p>Lorem ipsum.</p>
    <p>Lorem ipsum dolor.</p>

But that should do exactly what you want without RegExp - which may summon Tony the Pony

What the code does is

  1. Gets the element whose innerHTML you want to change
  2. gets the img with id="ytimg" within that element (a bit redundant as there's only ever one element with that ID in a page
  3. splits the src attribute (URL) of the image on the / character
  4. pops and discards the last part of the URL
  5. pops the second last part, the part you want and replaces the innerHTML with that part of the URL
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download