javascript - jQuery Each function iterates over everything despite if statement -
the code appends page url after clicking specific links on page. when run code, performs expected, appending url links match code. however, when print console, noticed appeared every single link on page, despite if statement supposed limit it. when took if statement out, code functioned same. however, though works, want make more efficient , run on links match parameters.
// function change link adding current page function setslatelink() { // url of current page var currentpage = document.location.href; // url of slate var slatelink = jquery('a[href$="code=magic"]').attr("href"); // rewrite slate, adding query string key slate , value of current page console.log("processing link change"); return slatelink += "&sys:interaction:summary=" + currentpage; } jquery(document).ready(function() { jquery("a").each(function(i, item) { // if there tag, check ends if (jquery(i == 'a[href$="code=magic"]')) { console.log("found link: " + jquery("a").attr("href")); // change link set referrer (current page) in url jquery('a[href$="magic"]').attr("href", setslatelink()); console.log("changed link"); } }); });
any idea cause this?
use is()
determine if element matched selector. http://api.jquery.com/is/
example:
if ($(item).is('a[href$="code=magic"]')) { // item matches }
but simplify selecting specific tags start with:
$('a[href$="code=magic"]').each(function(i, item) { // code=magic links... });
going 1 step further...
$('a[href$="code=magic"]').each(function(i, item) { var href = $(item).attr('href'); $(item).attr('href', href + '&sys:interaction:summary=' + document.location.href); });
Comments
Post a Comment