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
    a house

    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.

                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
    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
    Feb 2010
    Problems and fixes:
    1. document.getElementbyId("imageURLInput"); should be document.getElementById("imageURLInput");
    2. elementWithImageURL.getAttribute("value"); should be elementWithImageURL.value

    If you debugged your code the first problem is identified for you. The second problem is less obvious but can be solved by stepping through your code.

  3. #3
    Join Date
    Oct 2010
    a house


    thanx a heap it was driving me nuts

    i feel stupid cause i'm terrible at using debuggers but if i had of just used one i
    could have avoided this pointless post

    i was taking me so long that i thought it had to be a problem with the code specifics

    i'm just starting out but it's no excuse. i think i'm going to have to pay more attentions to learning debugging operation


Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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