0

I have span tag inside of that tag, new image tag is dynamically generated but I need to get that image src of that tag is it possible

<span id ="Imagespan">
    <image scr="some.png" alt="some" />
<span>  
l.hussain
  • 462
  • 3
  • 11
adikitty CB
  • 57
  • 1
  • 1
  • 8

2 Answers2

1

You mean img instead of image, yes you can, try :

$('#Imagespan').find('img').attr('src');
//or also
$('#Imagespan').find('img').prop('src');

Or use direct selector like :

$('#Imagespan img').prop('src');

Note: doesn't matter if the element is dynamically created or not, the method of getting the 'attribute/property' is the same.

.attr() changes attributes for that HTML tag.

.prop() changes properties for that HTML tag as per the DOM tree.

As the example in this link suggests. An input field can have the attribute "value". This will equal the default value you entered. If the user changes the value in the input field, the property "value" changes in the DOM Tree, but the original attribute is left remaining.

Source

Community
  • 1
  • 1
l.hussain
  • 462
  • 3
  • 11
  • 1
    can you explain the up/downs of prop vs attr? – dandavis Dec 07 '16 at 17:35
  • 1
    Already covered in other stackOverflow posts, i've added an explanation.. thanks for your intervention here. – l.hussain Dec 07 '16 at 17:39
  • 1
    It should also be noted that it doesn't matter if the element is dynamically created or not; the method of getting the attribute/property is the same. – Heretic Monkey Dec 07 '16 at 17:41
  • i meant for this specific use-case (img src), not in general: mainly, that `.prop` always returns a full URL and `.attr` (potentially) returns a relative URL... – dandavis Dec 07 '16 at 17:44
0

If you are using jquery then the below code gives you the source of dynamically created image

jQuery('#Imagespan img').attr('src')

ravi kanth
  • 36
  • 2