0

I am trying to replace the contents of the alt="" attribute in the tag.

The replacment text comes from textarea input that is assigned to var alttext

The var oldtext contains tags with placeholders for replacing, like:

<img alt="placeholder" scr="pic.jpg" />

The placeholder needs to be replaced the contents of var alttext.

So far I have tried:

function replacer() {
    var alttext = document.myform.alttext.value; 
    var oldtext = document.myform.oldtext.value;
    var replacedtext = oldtext.replace("placeholder", 'alttext' )
    document.myform.outputtext.value = replacedtext;        
}

But it does not work.

How can the alttext variable contents be used to replace the placeholder?

Thank you very much to everyone!

1
  • 3
    oldtext.replace("placeholder", alttext). Note the lack of quotes. 'alttext' is a literal string, alttext is a variable. Commented Feb 28, 2013 at 17:45

1 Answer 1

3
function replacer() {
   var alttext = document.myform.alttext.value; 
   var oldtext = document.myform.oldtext.value;
   var replacedtext = oldtext.replace("placeholder", alttext);
   document.myform.outputtext.value = replacedtext;        
}

you were trying to replace with quotes around your variable (alttext) making it a string literal

Sign up to request clarification or add additional context in comments.

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.