0

I'm using this javascript function to use a form to insert text into a set div.

I would like to be able to put an image url into a form and use this function to insert the url into an img tag instead of a div. What way could I achieve this?

Javascript:

function header01(){

    var head01v = document.getElementById('head01i').value;

    document.getElementById('head01c').innerHTML = head01v;
    document.getElementById('head01c').style.display = "block";

    return false;
}

I attempted to use the suggested solution, inputting the appropriate fields

 function image01(){

        var img = document.createElement('img');
        img.src = document.getElementById('imag01i').value;
        document.getElementById('imag01c').appendChild(img);

        return false;
    }

But to no avail, any additional help is greatly received.

1
  • var img = document.createElement('img'); img.src = theValueOfTheForm; Could that be of help? Commented Dec 16, 2012 at 18:56

1 Answer 1

1

Extending my comment to an answer:

It isn't that hard. First, check if you have a valid URL (more info on that here - first hit on Google), next, do this:

var img = document.createElement('img');
img.src = [the validated URL you obtained from the form];
document.getElementById('theElementYouWantToInsertTheImgIn').appendChild(img);
Sign up to request clarification or add additional context in comments.

4 Comments

thanks but attempted with no joy, have updated above question to what I tried.
What happens when you try my solution? @user1739696
Did the function actualiteit get called?
all is being used at barbsbristol.co.uk, I have updated it with the your code suggestion, any ideas?

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.