How to get div attribute without id

My site contains many images, and therefore I can’t give an identifier for each image, it will be a tedious task. I need to get the source attribute of an image tag, but I cannot do this without an ID. I tried using attr () and getAttribute (), but it does not work for me.

My code

<img src="./images/image1.jpg" width='100' height='100' alt='Sample image' onClick='imageInfo(this);'> <script> function imageInfo() { alert(this.src); } </script> 

I try to get the source of the image tag, but it does not come up. I also tried in jsfiddle, but it does not work.

JS FIDDLE LINK

+4
source share
15 answers

try something like that. Since you passed the image object but did not use it in the function

 <script> function imageInfo(obj) { alert(obj.src); } </script> 
+6
source

From your violin

 function imageInfo(this) { alert('image'); alert(this.src); } 

This happened because this is a reserved keyword .

  function imageInfo (e) { alert('image'); alert(e.src); } 

Also onclick not onclick , to find out the reason check this SO answer p>

check out this feed

+5
source

You can always do this:

HTML

 <img src="https://www.google.ca/images/srpr/logo4w.png" width='100' height='100' alt="Sample image"> 

Javascript

 $('img').click(function() { window.alert($(this).attr('src')); }); 

http://jsfiddle.net/QB6Da/

+3
source

To just fix your code, you need to pass a control to the javascript function:

 <img src="./images/image1.jpg" width='100' height='100' alt='Sample image' onClick='imageInfo(this);'> <script> function imageInfo(control) { alert(control.src); } </script> 
+2
source

In your example, you should put the Clicked object as a parameter for the function:

 function imageInfo(img) { alert(img.src); } 
+2
source

Since you marked this as jQuery, this should work:

http://jsfiddle.net/LgLzh/20/

 $(function() { $('img').each(function() { console.log($(this).attr('src')); }); }); 

Or this if you want to respond when they click on it: http://jsfiddle.net/LgLzh/26/

 $(function() { $('img').on('click', function() { console.log($(this).attr('src')); }); }); 
+1
source

Try it. It will help you

  $('img').click(function() { alert(this.src); }); 

Here demo

+1
source

This will give you src each image clicked on the page. However, for this you need jQuery.

 $('img').on('click', function (event) { console.log(event.target.src); }); 

http://jsfiddle.net/LgLzh/42/

+1
source

try it

  $('img').on('click',function(){ alert($(this).attr('src')); }); 
+1
source

Using onclick function in all img tags not a good method as you would have a nightmare if you made a small change. Add a class, and then using jquery you can get this to work. See the following.

Using the class attribute:

 <img src="http://4.bp.blogspot.com/-uyvTMZ3dFPs/UKHsc_ZbysI/AAAAAAAACvo/QdVAlVBbUxE/s320/1hello.jpg" class="image"/><br> <img src="http://hearstcommerce.ca/customcontent/members/premium/sample.jpg" class="image" /><br> <img src="http://www.find-a-job-online.org/images/picture.gif" class="image"/> 

Script:

 $(".image").click(function(){ var src = $(this).attr('src'); alert("SRC = " + src); }); 

Fiddle: http://jsfiddle.net/yL5cf/6/

+1
source
 <img src="http://4.bp.blogspot.com/-uyvTMZ3dFPs/UKHsc_ZbysI/AAAAAAAACvo/QdVAlVBbUxE/s320/1hello.jpg" width='400' height='400' alt='Sample_image' class="img"> 

the other answers are short and correct, but you want to use jQuery ...

 $(document).ready(function(){ function imageInfo(img) { var src = $(img).attr("src"); alert(src); }; $(".img").click(function(){ imageInfo(this); }); }); 

as well as "this" is a reserved keyword.

+1
source

http://jsfiddle.net/LgLzh/25/

For several reasons, you must define a function in the window area.

 window.imageInfo = function (source) { alert('image'); alert(source); } 
0
source

try it

 <img src="http://4.bp.blogspot.com/-uyvTMZ3dFPs/UKHsc_ZbysI/AAAAAAAACvo/QdVAlVBbUxE/s320/1hello.jpg" width='400' height='400' alt='Sample_image'> $(document).ready(function(){ $('img').click(function(e){ alert($(this).attr('src')); }); }); 

Remember to enable jquery to run this. Here's the fiddle http://jsfiddle.net/LgLzh/44/

0
source

This is what you can do ...

$('img').bind('click',function(){ console.log($(this).attr('src')); });

0
source

check the following js script

http://jsfiddle.net/LgLzh/22/

 $('img').filter(function(){ alert($(this).attr('src')); }); 

This is what you want ... If you can not be more specific?

-one
source

Source: https://habr.com/ru/post/1499307/


All Articles