Archive for the ‘jQuery’ Category

jQuery select anchor tags that contain specific text, then change id, onClick (Capitalized!!!!), href, :contains, .attr(), .each()

Wednesday, November 17th, 2010

Easy actually, on load, select an anchor tag (or several, and do the following for .each()), that contains, among other contents, the word ‘Read’,

then set its id attribute, a Mouse event, and alter its href value:

$(document).ready(function(){

$(“a:contains(‘Read’)”).each(function(){

$(this).attr(‘id‘,’newid’);

$(this).attr(‘onClick‘, ‘return false;’); // Capitalize the C!!!!

$(this).attr(‘href‘, ‘http://www.alexyz.com’);

});

});

// alternatively, line them all up like this:

$(this).attr(‘id‘,’newid’).attr(‘onClick‘,’return false;’).attr(‘href‘,’http://www.alexyz.com’);

 

// UPDATE: Better method

$(“a[href*='search phrase']“).each(function(){
var postId = $(this).attr(“href”).split(‘?’)[1].split(‘=’)[1].split(‘#’)[0]; // of course this will vary by need
$(this).attr(“href“,”?version=full&source=folder&post=”+postId);
});

Support Prototype script.aculo.us and jQuery, page built with Prototype now being pulled into page that uses jQuery

Tuesday, November 2nd, 2010

So I developed a handful of sites several years ago using Prototype and script.aculo.us,

but now they’re being pulled into/included/required by pages, a site, that is strictly jQuery,

so I need to amend the Prototype site’s functions to check which library is loaded and then act accordingly.

It begs the question, if I were doing this completely fresh, which pattern (see Design Pattern posts) would I attempt to employ…? For example, I’d love to have the option right now to simply write an adapter pattern function instead of burying this conditional within each function…

Aside from Namespacing either or both libraries of course…

Example:

<script>
accordianing = false;
function comments_cabinet()
{
if(accordianing == true)
{
setTimeout( function() { accordian(); }, 25);
}
else
{
accordianing = true;
/* scriptaculous */
if(window.Prototype)
{

if($(‘comments_button’).style.display == ‘none’) // notice the Prototype standard $(”)
{
new Effect.BlindDown(‘comments_button’, {
duration: 0.5,
afterFinish: function(){
accordianing = false;
}
});
}
else
{
new Effect.BlindUp(‘comments_button’, {
duration: 0.5,
afterFinish: function(){
accordianing = false;
}
});
}
}
else
{

$(‘#comments_button’).toggle(‘slow’); // otherwise, use the jQuery version $(‘#’)
accordianing = false;
}
return false;
}
}
</script>

Close Fancybox from within, old (1.2) and new (1.3)

Thursday, July 1st, 2010

In 1.2 this worked:

onclick=”$(“#fancy_outer”,window.parent.document).hide(); $(“#fancy_overlay”,window.parent.document).hide();

1.3+ this has changed to:

onclick=”parent.$.fancybox.close();

jQuery replace all img tags with certain source…

Monday, June 7th, 2010

$(‘img’).each(function(){
var t=$(this);
var src1 = t.attr(‘src’);
var newSrc = “images/play2.png”;
if(src1 == “images/play.png”)
{
$(this).attr(‘src’, newSrc);
}
});

jQuery .css background-image / .attr src examples

Monday, June 7th, 2010

$(“body”).css(“background-image”, “url(images/bg2.jpg)”);
$(“#header”).css(“background-image”, “url(images/bg-header2.png)”);
$(“#browse”).css(“background-image”, “url(images/bg-browse2.png)”);
$(“#paginate-slider2″).css(“background-image”, “url(images/bg-slide-thumbs2.jpg)”);
$(“#slides”).css(“background-image”, “url(images/bg-slides2.png)”);
$(“#ST-now”).css(“background-image”, “url(images/bg-STnow2.png)”);
$(“#searchBox”).attr(“src”, “images/search-box2.jpg”);
$(“#keywordsBox”).attr(“src”, “images/keywords2.jpg”);
$(“#inBox”).attr(“src”, “images/products2.jpg”);