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('<span>You have selected Google</span>');
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('<span>You have selected Google</span>');
If you have this HTML:
<a href="http://www.google.com">Google</a>
Then your selector will work:
$('a[href="http://www.google.com"]').prepend('<span>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();
});
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">Click</a>
<button id="btn1">Click me</button>
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("<p>Hello!</p>");
});
</script>
</head>
<body>
<a href="http://www.google.com/">Google</a>
<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.