views:

260

answers:

4

Hi,
How to find all the anchor tags inside a div tag and need to append one more property to the anchor tag. say target="_blank".

How can i do this in using jquery?

+2  A: 

Do you mean all the anchors in any div element, or in some specific div element? If it's the former, this will do the trick:

$('div a').attr('target', '_blank');

If it's a particular div you're interested in, give that element an id attribute and then use it like so:

$('#divid a').attr('target', '_blank');

replacing "divid" with the id in question.

Can you be more specific about what exactly you want?

In any case, you're going to need to use the attr() function to set the attribute; see the documentation here.

Syntactic
i have one div tag, in which i need to find all the href tags and add the attribute target="_blank". I tried this, but is not working. name of my div is "divLogo". am i missing something? My master page has the jquery script reference.
Nimesh
The name of your div, or the id of your div? There's a big difference. If your div has an `id` attribute of `divLogo`, then you'd use `$('#divLogo a').attr('target', '_blank')` to do this. The `#` symbol is telling the function to look for an element with an `id` of `divLogo`. The `a` separated by a space tells it to look for `a` elements that are within that element.
Syntactic
I suggest you read up on jQuery selectors here: http://api.jquery.com/category/selectors/
Syntactic
A: 
$('div a').attr('target','_blank');
Soufiane Hassou
+1  A: 
jQuery("#yourDIV a").attr("target", "_blank");

Have you read the jQuery docs? This can't be more basic.

RoToRa
+2  A: 

You are probably interested in a single div, not every div on page, so You have to identify it.

$('div#theDivsId a').attr('target','_blank');

or

$('div.theDivsClass a').attr('target','_blank');

PS. read documentation. it's easy.

naugtur