2

I'm trying to create a link within a blockquote element with jQuery. Right now I'm at this stage:

var pullQuote = $('span.pull-quote').each(function(){

    var $this = $(this),
    hrefLink = 'http://example.com',
    text = $this.text();

    $('<blockquote>', { 
        class: 'quote',
        text: text 
    }).prependTo( $this.closest('p'));

});

That creates the blockquote element with the text dynamical but I want to turn the text into link inside of the blockquote. The href isn't going to change so I can set that in a variable like I already have it.

Can I add something that will create the a tag inside of the blockquote to where I can still use the set variables? (Which is what I've been trying to do) Or do I need to just run this function and then create a new function which would handle adding the link?

2 Answers 2

3
$('<blockquote/>', { 
    class: 'quote',
    html: $('<a/>', {
        text: text,
        href: hrefLink
    )}
}).prependTo( $this.closest('p'));

If I understood you right, you just wanted to construct an anchor element within the blockquote and give that the text and the link.

Sign up to request clarification or add additional context in comments.

Comments

0
var $this = $(this);
hrefLink = 'http://example.com';
text = $this.text();

var blockQ=$('<blockquote class="quote">
                           <a href="'+hrefLink+'">'+text+'</blockquote>');
blockQ.prependTo( $this.closest('p'));

Demo : http://jsfiddle.net/QGtYQ/5/

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.