Paul Wichy
Paul Wichy

Reputation: 47

modify url_for, a href for Symfony with JQUERY

I have :

<span id="test"><a href="<?php echo url_for('mymodule/myaction?id='. $id) ?>">link1</a></span> <br />

or

<span id="test"><a href="<?php echo url_for('mymodule/myaction?id=') ?>" $id>link2</a></span>

I must generate url with url_for. i would like modify $id with javascript (jquery) if i click:

<span class="click" id=1>click one</span> <br />
<span class="click" id=2>click two</span> <br />
<span class="click" id=3>click three</span> <br />

if i click "CLICK ONE" the i would like link:

<span id="test"><a href="<?php echo url_for('mymodule/myaction?id=1') ?>">link1</a></span>

OR

<span id="test"><a href="<?php echo url_for('mymodule/myaction?id=') ?>"1>link2</a></span>

LIVE: http://jsfiddle.net/wtAbp/

Upvotes: 1

Views: 694

Answers (2)

Burgi
Burgi

Reputation: 1382

You could put each URL in a ref attribute for each span, like so:

<span class="click" id=1 rel="<?php echo url_for('mymodule/myaction?id=1') ?>">click one</span>
<span class="click" id=2 rel="<?php echo url_for('mymodule/myaction?id=2') ?>">click two</span>
<span class="click" id=3 rel="<?php echo url_for('mymodule/myaction?id=3') ?>">click three</span>

And then in your click event (or whichever way you chose), replace the target's href with the clicked element's rel value.

$('.click').unbind('click').click(function() {
  $('a#your_id').attr('href', $(this).attr('rel'));
});

Upvotes: 1

Jean-Charles
Jean-Charles

Reputation: 1720

Your code <?php echo url_for('mymodule/myaction?id=1') ?> is interpreted on the server side. So you can't modify it on the client side.

Just put "<?php echo url_for('mymodule/myaction?id=') ?>" in the href, without the id, and add dynamicaly the id :

Something like :

$(".click").live('click', function() {    
    $("a").attr('href',$("a").attr('href')+$(this).attr('id'));
});

Upvotes: 1

Related Questions