views:

418

answers:

4

This is a simplified version of my problem.

I have two buttons, and one image. The image code is something like this

<img class="onoff" src="image.jpg">

When I press button one I want the image to be wrapped in an A tag, like

<a href="link.html">
<img class="onoff" src="image.jpg">
</a>

And when I press the other button, the A tags should be removed.

What's the easiest way of doing this with jQuery?

A: 

you can use jQuery wrap() function.

The code is:

    <input type="button" id="button1" value="Wrap" />
    <input type="button" id="button2" value="Unwrap" />
    <img class="onoff" src="image.jpg" alt="" />

    $(function() {
        //wrap
        $('#button1').click(function() {
            $('.onoff').wrap('<a href="link.html"></a>');
            //if you want to make sure multiple clicks won't add new <a> around it
            //you could unbind this event like that:
            //$(this).unbind( "click" )
        });
        //unwrap
        $('#button2').click(function() {
            $('.onoff').parent().each(function() { $(this.childNodes).insertBefore(this); }).remove();
           //$(this).unbind( "click" )
        });
    });
rochal
Multiple clicks on button1 ==> multiple a's around the img!
Bruno Reis
fair point, but I see no item in the requirements saying this is not allowed. Maybe he does want to add multiple links? However, I have edited my code and added option to unbind event on click.
rochal
There's a new potential problem: uncomment both unbinds and then click on `button1` then on `button2` ==> no way to insert the `<a>` tag again.
Bruno Reis
But the way of the implementation is outside of the scope for this task. What matters here is `wrap` and 'Unwrap' function, how he wants to use it - it is not our problem. What if he DOES NOT want to use it twice? We don't know it, so I think we should focus on the TASK.
rochal
I think we should not limit ourselves strictly to what we can directly see. We should aim to do the best we can, even more so if this is so simple and takes almost no time. So, if he DOES NOT want to use it, by showing him how to do it he will at least have learned it for some day later -- and also the same technique he might want to apply elsewhere. Furthermore, there might be people with SIMILAR problems but with some extra requirements, and since SO is a kind of reference site for such questions, it would be interesting to have extrapolations around. Don't you agree?
Bruno Reis
+1  A: 

Try something like this:

$("img.onoff").wrap(
    $("<a/>").attr("href", "link.html"));

But perhaps using jQuery's click binding on the image itself would be better than wrapping the image in an anchor.

Andrew Hare
+5  A: 

To wrap the element

$(".onoff").wrap("<a href='link.html'></a>");

And to unwrap

$(".onoff").parent().replaceWith($(".onoff"));
Simon Fox
Cool use of replaceWith, Simon. Thanks!
btelles
A: 

Hi, you have already many answers, but (at least before I started writing) none of them will correctly work.

They do not take into account that you should not wrap the <img> with multiple <a> tags. Furthermore, do not try to unwrap it if it is not wrapped! You would destroy your DOM.

This code simple does a verification before wrapping or unwrapping:

$(function(){
    var wrapped = false;
    var original = $(".onoff");

    $("#button1").click(function(){
        if (!wrapped) {
            wrapped = true;
            $(".onoff").wrap("<a href=\"link.html\"></a>");
        }
    });

    $("#button2").click(function(){
        if (wrapped) {
            wrapped = false;
            $(".onoff").parent().replaceWith(original);
        }
    });
});

Good luck!

Bruno Reis