views:

52

answers:

3

I am trying t o create img captions based on src value match.

JQUERY : What is the best way to extract "Author-ABC" from an img with src value wwww.abcd.com/images/imagename_Author-ABC_.jpg and replace the alt value with this value.

DRUPAL : Is there a way to preprocess this a drupal template function and save the value in img alt attribute?

Ideas? Basho

+2  A: 

Assuming that your image name always starts with imagename_ and ends with _.extension, you could do something like this:

var src = jQuery("#imgId").attr("src");
var imgName = src.replace(/^.*imagename_/,"").replace(/_\.[a-z]+$/, "");

Or, assuming that the URL itself will not contain any underscores, you can do this:

var src = jQuery("#imgId").attr("src");
var imgName = src.replace(/^[^_]+_/,"").replace(/_\.[a-z]+$/, "");
Vivin Paliath
A: 

Thanks Vivin,

The image name does not start with 'imagename' but will have a "_Author_ChangingValue_.jpg' suffix for all arc values. How can I filter images with this string '_Author_ChangingValue_' in their src and replace their img alt attr ?

thanks, Basho

+1  A: 

Instead of doing this with JavaScript you could instead hook into Drupal and do all of this when the image is created. I'm guessing you are using a CCK field on a node. With a hook_form_alter you can add a submit handler for the form. In it you could do the regex needed to pull out the author name from the img file name and add it as alt attribute.

Doing this you get the markup you want upon creation instead of having to depend on javescript to alter it. This is the flexibility that makes Drupal great and is the Drupal way of doing this.

googletorp