Return to Snippet

Revision: 16284
at July 31, 2009 09:12 by dougunderscorenelson


Updated Code
$.fn.anchorAway = function(act){
  urlArr    = window.location.href.split("#");
  anchorName  = urlArr[(urlArr.length - 1)];
  $(this).each(function(){
		targetId  = $(this).attr("id");
		bindIt = function(i){
			$("a[href='#" + i + "']").each(function(){
				$(this).bind('click', function(){
					act($("#" + i));
				});
			});
		}
		bindIt(targetId);
		if (targetId == anchorName) {
			act($(this));
		}
	});
}


=== USAGE ===

$("div").anchorAway(function(obj){
  obj.css("background-color","yellow");
});

Revision: 16283
at July 30, 2009 21:29 by dougunderscorenelson


Updated Code
$.fn.anchorAway = function(act){

  urlArr    = window.location.href.split("#");
  anchorName  = urlArr[(urlArr.length - 1)];

  $(this).each(function(){
    targetId  = $(this).attr("id");	
    bindIt = function(i){
      $("a[href='#" + i + "']").each(function(e){
        e.bind('click', function(){
          act($("#" + i));
        });
      });
    }
    bindIt(targetId);
    if (targetId == anchorName) {
      act($(this));
    }
  });
}

=== USAGE ===

$("div").anchorAway(function(obj){
  obj.css("background-color","yellow");
});

Revision: 16282
at July 30, 2009 11:35 by dougunderscorenelson


Updated Code
$.fn.anchorAway = function(act){

  urlArr    = window.location.href.split("#");
  anchorName  = urlArr[(urlArr.length - 1)];

  $(this).each(function(){
		
		targetId  = $(this).attr("id");
		
		bindIt = function(i){
			$("a[href='#" + i + "']").each(function(e){
				e.bind('click', function(){
					act($("#" + i));
				});
			});
		}
		bindIt(targetId);
	
		if (targetId == anchorName) {
			act($(this));
		}

	});

}

=== USAGE ===

  $("div").anchorAway(function(obj){
    obj.css("background-color","white");
  });

Revision: 16281
at July 30, 2009 11:34 by dougunderscorenelson


Updated Code
$.fn.anchorAway = function(act){

	urlArr    = window.location.href.split("#");
	anchorName  = urlArr[(urlArr.length - 1)];

	$(this).each(function(){
		
		targetId  = $(this).attr("id");
		
	  bindIt = function(i){
			$("a[href='#" + i + "']").each(function(e){
				e.bind('click', function(){
			    act($("#" + i));
				});
			});
		}
		bindIt(targetId);
	
		if (targetId == anchorName) {
			act($(this));
		}

	});

}

=== USAGE ===

  $("div").anchorAway(function(obj){
    obj.css("background-color","white");
  });

Revision: 16280
at July 30, 2009 11:33 by dougunderscorenelson


Updated Code
$.fn.anchorAway = function(act){

	urlArr    = window.location.href.split("#");
	anchorName  = urlArr[(urlArr.length - 1)];

	$(this).each(function(){
		
		targetId  = $(this).attr("id");
		
	  bindIt = function(i){
			$("a[href='#" + i + "']").each(function(e){
				e.bind('click', function(){
			    act($("#" + i));
				});
			});
		}
		bindIt(targetId);
	
		if (targetId == anchorName) {
			act($(this));
		}

	});

}

=== USAGE ===


  $("div").anchorAway(function(obj){
	  obj.css("background-color","white");
	});

Revision: 16279
at July 30, 2009 11:26 by dougunderscorenelson


Updated Code
$.fn.anchorAway = function(f){

	a  = window.location.href.split("#");
	p  = a[(a.length - 1)];

	$(this).each(function(){
		
		i  = $(this).attr("id");
		
	  bindIt = function(i){
			$("a[href='#" + i + "']").bind('click', function(){
		    f($("#" + i));
			});
		}
		bindIt(i);
	
		if (i == p) {
			f($(this));
		}

	});

}

=== USAGE ===

$("div.anchorme").anchorAway(function(b){
  b.addClass("highlighted");
});

Revision: 16278
at July 30, 2009 11:25 by dougunderscorenelson


Updated Code
$.fn.anchorAway = function(f){

	a  = window.location.href.split("#");
	p  = a[(a.length - 1)];

	$(this).each(function(x){
		
		i  = $(this).attr("id");
		
	  bindIt = function(i){
			$("a[href='#" + i + "']").bind('click', function(){
		    f($("#" + i));
			});
		}
		bindIt(i);
	
		if (i == p) {
			f($(this));
		}

	});

}

=== USAGE ===

$("div.anchorme").anchorAway(function(b){
  b.addClass("highlighted");
});

Revision: 16277
at July 30, 2009 10:44 by dougunderscorenelson


Initial Code
$.fn.anchorAway = function(f){
	a  = window.location.href.split("#");
	i  = a[(a.length - 1)];
	$(this).each(function(){
		if ($(this).attr("id") == i) {
			f($(this));
		}
	});
}

=== USAGE ===

$("div.anchorme").anchorAway(function(b){
  b.addClass("highlighted");
});

Initial URL


Initial Description
Sometimes you click a link like faq.html#item5 and, after the browser jumps you to the anchor, you have trouble spotting the item you're looking for. anchorAway is a little function that helps you call a function on anchored items, maybe something like a scrollTo or a background color, that helps users easily spot the content you directed them to.

anchorAway parses the window.location for an anchor (#foo), finds it, and calls the function (act(bar)) on it. It also finds any on-page links to the objects you've specified and binds the same function to their click event.

Initial Title
Call a function on an anchored object

Initial Tags
jquery

Initial Language
jQuery