how to get img src value without img id? (with javascript)

ababababa<img src="http://example.com/blabla/bego.png">ababababa

Recommended Answers

getElementsByTagName() would give you a list of nodes.

Jump to Post

That's not an error, it's a list.

Jump to Post

All 5 Replies

it gives me error :
[object HTMLCollection]

Once you have the items in the collection, you can access the items. Here is an example that should get you going...

<body>

<img src="images/image1.jpg" />
<img src="images/image2.jpg" />

<script>
    var images = document.getElementsByTagName("img"); 
    for (var i = 0; i < images.length; i++) { 
        var source = images[i].getAttribute("src"); 
        console.log(source)
        }

</script>

</body>

The output in the JavaScript console would be...

images/image1.jpg
images/image2.jpg

got it now, getElementsByTagName(), mr. JorgeM you are right. thank you. thread close.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.