views:

214

answers:

4

Within an anchor, this works:

$(this).prepend('<span>This is the span content</span>');

when trying to do if href equal to, it does not work

$('a[href="http://www.google.com"]').prepend('&lt;span&gt;You have selected Google</span>');
+1  A: 

If you have this HTML:

<a href="http://www.google.com"&gt;Google&lt;/a&gt;

Then your selector will work:

$('a[href="http://www.google.com"]').prepend('&lt;span&gt;You have selected Google</span>');

If you wanted to follow the strict escaping guidelines on the jQuery website, then it would look like this:

$('a[href="http\\:\\/\\/www\\.google\\.com"]').prepend('<span>You have selected Google</span>');

I suspect, you are possibly wanting something like this which applies the concept in an if statement, in a click event:

$("a").click(function(e){
    var $a = $(this);
    if($a.is('[href="http://www.google.com"]')){
       $a.prepend('<span>You have selected Google</span>');
    }
    e.preventDefault();
});
Doug Neiner
+3  A: 

I have done a sample one and didn't find any error.

<script>
$(function(){
    $("#btn1").click ( function() {
        $("a[href='http://www.google.com']").prepend ( "<span>You have selected Google</span>" );
    });
});
</script>
<a href="http://www.google.com"&gt;Click&lt;/a&gt;
<button id="btn1">Click me</button>

Working Demo

rahul
+1 for working editable demo jsbin rocks!
Hogan
A: 

It should work. Try the following HTML, for me in Firefox 3.5.6 on a Mac it works.

<!DOCTYPE html>
<html>
    <head>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"
            type="text/javascript"></script>
        <script type="text/javascript">
            $(function(){
                $('a[href="http://www.google.com/"]').prepend("&lt;p&gt;Hello!&lt;/p&gt;");
            });
        </script>
    </head>
    <body>
        <a href="http://www.google.com/"&gt;Google&lt;/a&gt;
        <a href="nowhereyouvebeen">No where you've been</a>
    </body>
</html>

Perhaps you have a trailing slash in one url but not the other: "http://www.google.com/" vs. "http://www.google.com"? The two have to be exactly the same.

Danny Roberts
A: 

the issue was with amp and & symbols

bocca