1

I’m using JQuery 1.7.1. I have a block of well-formed HTML stored in a variable. Within this HTML there is a line,

<img id=“my-img” src=“…” />

Within my variable, how do I replace the value of the src attribute with something else?

1
  • 1
    By the way, you have some funky quote characters in there that could cause issues down the road. Don't write code in Word. ;-) Commented Sep 25, 2014 at 16:28

5 Answers 5

2

http://api.jquery.com/prop/

var imgElement = $(htmlString).find("#my-img");
imgElement.prop('src', 'something else')`;
Sign up to request clarification or add additional context in comments.

3 Comments

This doesn't answer the question, which was about changing the property within a variable.
@isherwood You are right, I read the question too quickly...my bad
@isherwood I see nothing wrong with the original version of this post.
2

jQuery can parse HTML strings for you, so you can work with it using jQuery API like you do when it's part of the document.

var htmlString = "<foo ... ></foo>";
var parsedHtml = $(htmlString);
parsedHtml.find('#my-img').prop('src', 'new value');
htmlString = parsedHtml.wrap('<div />').parent().html(); // in case you need it as string again 

8 Comments

Hey, the line "var parsedHtml = $(htmlString);" is stripping out the outermost HTML element (in your example, "<foo>"). Is tehre a way to adjust the above so that the original HTML is preserved, save the replacing of the image attribute?
@Dave that's how jQuery.find works, it only look for childrens. You can use the method described in this answer, or use a dummy element $('<div />').append(parsedHtml).find()
That's great that find doesn't work that way, but my question is not about find, it is about searching and replacing a particular value. You don't have to use find in the answer, but again, the above code does search and replace my source attribute but it changes other things about the variable's value that I wish to remain the same.
@Dave, this shouldn't change anything else that I'm aware of. Can you be more specific about what changes?
If you run the above code as you suggest, the last line, "htmlString = parsedHtml.html();'<, the variable "htmlString" does not contain the "<foo>" tags. I'm looking for a solution that does include the "<foo>" tags and also changes the src of the img within the HTML. Does that make sense?
|
0

You wouldn't do a string replace. You'd access the object and change the attribute:

$(myVar).find('#my-img').attr('src', 'my-new-src');

Comments

0
var html = $('<tag>...<img id="my-img" src="" />...</tag>');
html.find('#my-img').attr('src', 'other src');
//or
//html.find('#my-img').prop('src', 'other src')
htmlFinal = html.html();
$('body').html(htmlFinal);

Comments

0

Went ahead and used a generic Javascript solution, which was

myHtml = myHtml.replace(/<img id=\"my-img\" src=\"(.*?)\" width=\"66\">/g, "<img id=\"my-img\" src=\"" + imgSrc + "\" width=\"66\">");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.