Results 1 to 3 of 3

Thread: how to get text input to be used as a reference to an image src

  1. #1
    Join Date
    Oct 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post how to get text input to be used as a reference to an image src

    I've been looking for a reference to my situation for two days and i can't find any that are relevant enough.

    I'm trying to accept text as a URL for an image source.
    I then display that image in a form with the use of a <button type=button onclick=codeThatGeneratesImage > type thing.

    Code:
                function addImageToForm(formID) {
                    //get a reference to the form
                    var formElement = document.getElementById(formID);
                    //get a reference to the element that contains the url
                    var elementWithImageURL = document.getElementbyId("imageURLInput");
                    //get URL entered
                    var imageURL = elementWithImageURL.getAttribute("value");
                    //create image element
    ----------------------------------------------------------------------------------
                    var newImage = document.createElement("img");
                    //reference the source using the url
                    newImage.setAttribute("src", imageURL);
                    newImage.setAttribute("name", "newImage");
                    //append the image to form
                    formElement.appendChild(newImage);
                }
    i know that the bottom half works because i tried it with just a reference to an image directly and it worked.

    i can't figure out why most of the top part isnt working.

    any help would be much appreciated

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    getAttribute() and setAttribute() are often not required and can in some cases not be or do what one expects. Assuming you have the markup to support all this, I would:

    Code:
                function addImageToForm(formID) {
                    //get a reference to the form
                    var formElement = document.getElementById(formID);
                    //get a reference to the element that contains the url
                    var elementWithImageURL = document.getElementbyId("imageURLInput");
                    //get URL entered
                    var imageURL = elementWithImageURL.value;
                    //create image element
    // ----------------------------------------------------------------------------------
                    var newImage = document.createElement("img");
                    //reference the source using the url
                    newImage.src = imageURL;
                    newImage.name = "newImage";
                    //append the image to form
                    formElement.appendChild(newImage);
                }
    If that still doesn't work, make sure the value of the element is as expected with an alert:

    Code:
                function addImageToForm(formID) {
                    //get a reference to the form
                    var formElement = document.getElementById(formID);
                    //get a reference to the element that contains the url
                    var elementWithImageURL = document.getElementbyId("imageURLInput");
                    //get URL entered
                    var imageURL = elementWithImageURL.value;
                    alert(imageURL);
                    //create image element
    // ----------------------------------------------------------------------------------
                    var newImage = document.createElement("img");
                    //reference the source using the url
                    newImage.src = imageURL;
                    newImage.name = "newImage";
                    //append the image to form
                    formElement.appendChild(newImage);
                }
    If you want more help I need to see the markup you are using this with. I also need to know what event runs this function. Perhaps the expected elements aren't available yet.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    Is "formID" being set correctly, if not sure try alerting it to confirm the value is as expected.
    Corrections to my coding/thoughts welcome.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •