$("a[href^=http://]").attr("target","_blank")



Description

This selects all links with an href attribute starting with http:// and set its target attribute to _blank.

Syntax

Here is the simple syntax to use this method −

selector.attr(target:inputValue)

Parameters

Here is the description of all the parameters used by this method −

  • target − This is the target property of the matched anchor element.

  • inputValue − This is the value of the property to be set.

Example

Following example would selects all links with an href attribute starting with http:// and set its target attribute to _blank.

<html>
   <head>
      <title>The jQuery Example</title>
      <script type = "text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js">
      </script>		
      <script type = "text/javascript" language = "javascript">
         $(document).ready(function() {
            $("a[href^='http://']").attr("target","_blank");
         });
      </script>
   </head>	
   
   <body>
      <div>
         <a href="http://www.google.com">Google</a> <br/> <br/><a href="http://www.yahoo.com">Yahoo</a>
      </div>
   </body>	
</html>

This will produce following result −

jqueryexamples_attributes.htm
Advertisements