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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jason Moyes
    New Member
    • Sep 2010
    • 2

    #1

    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=codeTha tGeneratesImage > 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 :)
  • RamananKalirajan
    Contributor
    • Mar 2008
    • 608

    #2
    Code:
     var imageURL = elementWithImageURL.getAttribute("value");
    replace this line with

    Code:
     var imageURL = elementWithImageURL.getAttribute("src");
    Thanks and Regards
    Ramanan Kalirajan

    Comment

    • Jason Moyes
      New Member
      • Sep 2010
      • 2

      #3
      Interesting. i gave it a try and it didn't work.

      i want that line of code to get what the user has written.

      I'm kinda new to javascript but elementWithImag eURL
      is a reference to my input element and as far as i know it doesn't have a src attribute.

      thanks for the reply tho
      any more ideas?

      Comment

      Working...