Friday, 2 October 2009

JQuery Example: Search and replace on a specific html attribute value

This snippet can be used to query for an element with a specific attribute value and replace that value. In the example below (for shadowbox), a rel attribute was set with "player=flv".

We were using swfobject to detect for the presence of a flash player [(swfobject.hasFlashPlayerVersion("8"))] in the event of flash player not being installed and javascript being enabled, I needed to be able to rewrite the rel attributes to get shadowbox to use a different player. This JQuery example worked nicely by allowing us to query the DOM for all anchor elements containing rel attributes which contained "flv" - and to replace all instances of "flv" with "qt":