tags:

views:

47

answers:

4

[edit] I am NOT using jquery in this app.

Looking for a way to force preexisting links to open in a new window. But I only want to Apply this behavior to links appearing in a specific div element that has a classname but no ID

+1  A: 

The simplest way to do this is to use jQuery:

$('div.SomeClass a').attr('target', '_blank');
SLaks
+1  A: 

If you are using jQuery you can do like this:

$(function(){
  $('.ClassNameOfTheDiv a').attr('target', '_blank');
});
Guffa
A: 

Try...

<a href="javascript:window.open('www.yoursite.org')>Your Site Opens in a new window</a>
cymorg
You're misunderstanding the question.
SLaks
welcome to SO cymorg, but as SLaks pointed out please try to make sure that you are answering the actual question being asked :)
jaywon
+2  A: 

If you're not using jQuery, first get the element that you want to apply it to.

var allDivs = document.getElementsByTagName('div');
var myDiv = null;

for (var j = 0; j < allDivs.length; j++) {
    var divClass = allDivs[j].getAttribute('class');
    if (divClass != null && divClass.indexof(THE_CLASS_NAME) >= 0) {
        myDiv = allDivs[i];
        var allAnchors = myDiv.getElementsByTagName('a');
        for (var i = 0; i < allAnchors.length; a++) {
            allAnchors[i].setAttribute('target', '_blank');
        }
    }
}
ferrari fan
I think you meant `allAnchors[i]`.
Joel Potter
@Joel Potter thanks!
ferrari fan
Notice how much simpler the jQuery way is. (This is the simplest cross-browser way to do it without jQuery or a similar library
SLaks
I'm not saying jQuery is a bad idea I'm just saying that it's not necessary. That also doesn't mean he can't use any other library of his choice, but I'm giving him an answer without dependence on a third party.
ferrari fan
Thanks FF! This is just what I was looking for.
Scott B
@FF > I'm getting Error: divClass.indexof is not a function, changed it to indexOf which resolved that error. Now getting myDiv is undefined ?
Scott B