-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?

pimvdb
  • 151,816
  • 78
  • 307
  • 352
Kandinski
  • 953
  • 11
  • 30
  • Not jQuery related. Basic JavaScript string concatenation. Please read https://developer.mozilla.org/en/JavaScript/Guide – Felix Kling Jun 26 '11 at 21:15
  • 2
    Why does your anchor have a `src` attribute? That's not valid according to [the standard](http://www.whatwg.org/specs/web-apps/current-work/#elements-1) – Šime Vidas Jun 26 '11 at 21:43

3 Answers3

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
    })
);
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 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 – Kandinski Jun 27 '11 at 12:07
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);
RichW
  • 10,692
  • 6
  • 26
  • 33
-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?>"/>');