kyung
kyung

Reputation: 23

How can add existing name used attr()

I want to change attr('src', 'imagename').

But Preserving the existing name.

<image src="abc">
<jquery>
 $('image).attr('src','' )  -> abcde
 and just add existing abc + de

How can I solve this problem?

thanks :)

Upvotes: 1

Views: 36

Answers (2)

Death-is-the-real-truth
Death-is-the-real-truth

Reputation: 72269

  1. the actual element is <img> not <image>

  2. Use the same code to get src first and then replace the value by adding an extra string to it.

Running example:

$('img').attr('src', $('img').attr('src')+'de');
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<img src="abc">

Upvotes: 1

Ram
Ram

Reputation: 144699

The element name is img not image. Here is one way of doing this:

$('img').attr('src', function(_, currentValue) {
    return currentValue + 'de';
});

Note that this affects all elements in the jQuery collection, i.e. this adds the "de" to all img elements' src attribute.

Upvotes: 1

Related Questions