views:

48

answers:

2

I'm trying to select a specfic < a > to put a style on it put my code is not working`

   var url=window.location.protocol + "//" + window.location.host+"/"+window.location.pathname;
jQuery('#accordion a[href*="'+url+'"]').css("font-weight", "bold");

that code is not working at all and it gives undefind object..so any help here :)

Best Regards M Hegab

A: 

window.location.pathname already starts with a /, I'm not exactly sure how your setup is, but it should be more like this (without the / between host and path):

var url=window.location.protocol + "//" + window.location.host+window.location.pathname;
jQuery('#accordion a[href*="'+url+'"]').css("font-weight", "bold");
Nick Craver
A: 

I guess attribute selectors work independently. This worked for me.

jQuery("#accordion").find("a[href*='" + url + "']").css("font-weight", "bold");

If you need to target only the child anchor tags, you can use the children() method instead of the find() method.

This is the working for me:

<html>
<head>
<title>Nithesh Chandra</title>
<style>
a.test { color: green; }
</style>
<script src="jquery.min.js" type="text/javascript" language="javascript">
</script>
<script language="javascript">
$(function() {
  $('#testDiv').find("a[href*='http://google.com/']").addClass('test');
});
</script>
</head>
<body>
<div id="testDiv">
  <a href="http://google.com/"&gt;Nithesh&lt;/a&gt;
</div>
</body>
</html>

Hope it helps.

Nithesh Chandra
actually i tried find and it works only if i select (a) otherwise it gives me nothing :( it seems the selection for the href is not working fine here
Mohamed Emad Hegab
try href without the *. I mean if you know the exact url, you can always match it as "Equals" rather than "Contains". Check http://visualjquery.com/ for more options.
Nithesh Chandra
no it's not always that it will be matching exactly.. the parameter may be switch the places from others or the parameters can get deferent values.. that's why i'm using like or contain
Mohamed Emad Hegab