Skip to content

Instantly share code, notes, and snippets.

@beatricebock
Created August 29, 2017 03:05
Show Gist options
  • Save beatricebock/0e54fb7041242bd7e3da8b04ce967f2b to your computer and use it in GitHub Desktop.
Save beatricebock/0e54fb7041242bd7e3da8b04ce967f2b to your computer and use it in GitHub Desktop.
jQuery snippet for automating adding "target"="_blank" to anchor tags
<!-- just copy-paste this snippet, paste it in your own file, edit it, then run it in your browser. Then, inspect the generated code and copy the generated html. No more copy-pasting code into your a tags and missing a few cos you're (i'm) a clutz. ezpz -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>
<!-- PLACE YOUR HTML HERE -->
<!-- Magic happens here -->
<script>
//Find each <a> tag and add the attribute target="_blank" to them
$("a").each(function() {
$(this).attr("target", "_blank");
})
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment