0

How can I programmatically change the following img in the structure below?

  <div id="runner" class="nav brand pull-left">
        <a alt="motherboard" href="/tree/">
            <img alt="mega node" src="img.png"></img>
        </a>
  </div>

firefox points me to this

#runner > a:nth-child(1) > img:nth-child(1)

I've tried using standard methods as the following with various naming conventions

document.getElementById("#runner").src="icon.png";
document.getElementById("#runner.a.img").src="icon.png";
document.getElementById("#runner.img").src="icon.png";
document.getElementById("mega node").src="icon.png";

I've even tried css with no avail,

div#runner {
    content:url("img.png");
}
potashin
  • 44,205
  • 11
  • 83
  • 107
pyCthon
  • 11,746
  • 20
  • 73
  • 135
  • 1
    I think this is not how you use getElementById. – Havenard May 24 '14 at 03:56
  • @Havenard I was following the example from this answer http://stackoverflow.com/questions/11722400/programmatically-change-the-src-of-an-img-tag – pyCthon May 24 '14 at 03:58
  • 2
    Nah, `getElementById()` will select a DOM element by the specified `id`, and `id` only. I suppose you want to use `querySelector()` instead. – Havenard May 24 '14 at 04:01
  • @Havenard ah i get it now, thanks, for science how would i do this with css? – pyCthon May 24 '14 at 04:02
  • 1
    http://stackoverflow.com/questions/2676436/define-an-imgs-src-attribute-in-css – Havenard May 24 '14 at 04:03

1 Answers1

5

Your are specifying argument in getElementById() in a wrong way, you should provide just an id, without selector #. You can find your #runner div and then find the first <img> tag in it and change it's src attribute :

  document.getElementById('runner').querySelector('img').src = 'icon.png' 

Example

Or even simpler, without using getElementById() :

 document.querySelector('#runner a img').src = 'icon.png'

Example

SaidbakR
  • 13,303
  • 20
  • 101
  • 195
potashin
  • 44,205
  • 11
  • 83
  • 107