0

I have a snippet that is like this:

<div id="image_input">
    <img src="missing-image.png" name="image_comp" />
    <input type="text" name="image_url" size="37" />
</div>

I want that when someone unfocus image_url or even do a onChange event, a Javascript function called changeImage() be raised(this I already know how to do, now comes what I don't) and change the image_comp changes to the URL that is written in image_url. How to do this?

2
  • maybe document.getElementByName('image_comp').src = document.getElementByName('image_url').value; Commented Jan 11, 2011 at 3:31
  • 2
    getElementByName is not a function, only getElementsByName. This is because the name does not need to be unique like id. getElementsByName returns an array of objects with the specified name. Commented Jan 11, 2011 at 3:34

3 Answers 3

4
document.getElementsByName("image_comp")[0].src = document.getElementsByName("image_url")[0].src;

if you have more elements with the same name you can loop trough them or use id's if you want to be sure it you do this for only 1 element.

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

Comments

1
<div id="image_input">
    <img src="missing-image.png" name="image_comp" id="image_comp" />
    <input type="text" name="image_url" size="37" id="image_url" />
</div>

You can define ids for both the elements and have this in your onChange function:

document.getElementById('image_comp').src = document.getElementById('image_url').value;

Comments

1
<div id="image_input">
    <img src="missing-image.png" id="image_comp" />
    <input type="text" name="image_url" id="image_url" size="37" />
</div>

<script type="text/javascript">
    jQuery('#image_url').bind('change blur', function () {
        jQuery('#image_comp').attr('src', this.value);
    });
</script>

1 Comment

that's what happens when you answer before having coffee :-P

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.