-1

I just want to insert the variable $src (it fetches the url of an image)

$src = $('a[class="button"]').attr("src");

into this string

$("a[class=panel]").append("--wanna insert $src here--");

How can I do this?

2
  • Not jQuery related. Basic JavaScript string concatenation. Please read developer.mozilla.org/en/JavaScript/Guide Commented Jun 26, 2011 at 21:15
  • 2
    Why does your anchor have a src attribute? That's not valid according to the standard Commented Jun 26, 2011 at 21:43

3 Answers 3

1
var $src = $('a.button').attr("src");

and then:

$('div.panel').append('<div class="zoom-btn"><a href="' + $src + '"></a></div>');

or personally I would prefer:

$('div.panel').append(
    $('<div/>', {
        'class': 'zoom-btn'
    }).append('<a/>', {
        href: $src
    })
);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Darin, the first I would prefer the first choice tough is much more readable to me ( and resembles php as well). Nice and clean idea. Now I would like to scan all the a.button links in the page ( I assume I need to use each() and for each $src fetched to display the structure in the div.panel. I tried but I got not luck. Could you help? Thanks
0

This answer isn't relevant now you've gone and completely changed the question after you've posted..

var src = $('a[class="button"]').attr("src");
$("div[class=panel]").append(src);

1 Comment

or a one-liner $("div[class=panel]").append($('a[class="button"]').attr("src"));
-1

You mentioned in your question that you want to insert variable $src which fetch the Url of an Image. and for Image we need

$src = $('a[class="button"] img').attr("src","<?=$src?>");

Anchor with "button" class containing image tag

<a href="#" class="button"><img src=""/></a>

and you can also use below code if you don't have element inside anchor.

$src = $('a[class="button"]').append('<img src="<?=$src?>"/>');

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.