Pack Hack
Pack Hack

Reputation: 109

Changing src value through jQuery

I have a jQuery variable merImg like this

var merImg = '<a href="http://xxx.com"><img src="http://om.com/pion/thumbnail/11x65/aa/img.jpg" border="0" align="left" height="11" width="65"></a>';

I want to replace thumbnail/11x65 in the src with image/40x using jQuery. Is there a regex to do this easier? Or any logic at all to change it?

Also I want to remove the height and width attribute to the img tag. How do i go about it?

Upvotes: 0

Views: 317

Answers (1)

Nicola Peluchetti
Nicola Peluchetti

Reputation: 76910

You could use the standard replace() method of javascript:

merImg = merImg.replace('thumbnail/11x65', 'image/40x');

as the first argument you can also pass a regExp like this

   merImg = merImg.replace(/your regexp/, 'image/40x');

To remove height and width in this case you could replace them with an empty string:

merImg = merImg.replace('width="65"', '');
merImg = merImg.replace('height="11"', '');

I'm no experto of regular experssions but you can write a general regular expression to strip away width and height attributes from a string.

Also a llot of people suggest (correctly) that you should use an HTML parser to parse HTMl. Try to google it if you need more info about it

But in this particulare what i've written should work

Upvotes: 2

Related Questions