www.webdeveloper.com
Results 1 to 5 of 5

Thread: [RESOLVED] error: is null or not an object???

  1. #1
    Join Date
    May 2007
    Location
    Surrey, UK
    Posts
    75

    resolved [RESOLVED] error: is null or not an object???

    This is my code

    Code:
    function select(star) {
    
        for (i = 0; i < 10; i++) {
            var id = i + 1;
            if (i < star.id) {
                var img = document.getElementById(id);
                img.setAttribute("src", "App_Themes/Main/Graphics/LJN_RATING_STAR_HOVER.jpg");
            }
            else {
                var img = document.getElementById(id);
                img.setAttribute("src", "App_Themes/Main/Graphics/LJN_RATING_STAR_GREY.jpg");
            }
        }
        var rating = document.getElementById("ratingSelected");
        rating.innerHTML = star.id + " Stars";
        var ratingInput = document.getElementById("ratingField");
        ratingInput.value = star.id;
        selected = true;
        numStar = star.id; 
    }
    Code:
    <input id="ratingField" type="text" visible="false" />
    the line that is causing the problem is

    Code:
        ratingInput.value = star.id;
    the function is simply called in an onclick event. you can ignore the for loop that all works fine, that just swaps some images in a list.

    but the line that is causing the problem is supposed to insert the id of the element that called the function into the input 'ratingField'

    What is wrong with this line of code? the debugger says 'is null or not an object'

    There is nothing wrong with star.id it is used elsewhere in the function and definately contains a value so I think the problem might be with the ratingInput.value part or the line

    var ratingInput = document.getElementById("ratingField");

    thank you
    Last edited by BrownWarrior; 04-04-2010 at 11:56 AM.

  2. #2
    Join Date
    Mar 2009
    Location
    Fortaleza, CE, Brazil
    Posts
    81
    I'm not sure, but try to change the field type from "text" to "hidden", get rid of that "visible" attribute, and try again.

    or

    put an
    Code:
    alert(ratingInput);
    to see if it's really null.

  3. #3
    Join Date
    May 2007
    Location
    Surrey, UK
    Posts
    75
    yes I added an alert after

    var ratingInput = document.getElementById("ratingField");
    alert(ratingInput);

    and it said ratingInput is null!!

    so that is definately the problem!

    why is - document.getElementById("ratingField"); returning null?

    I have a hunch that it is because it is a nested element within a form but I thought that document.getElemetById() got any element within the document?

  4. #4
    Join Date
    May 2007
    Location
    Surrey, UK
    Posts
    75
    okay, one thing I missed out is the line

    <input id="ratingFiled" type="text" visible="false" />

    has one more attribute..

    runat="sever"

    so that I can access it programatically in my aspx backend.

    it seems that if I remove this runat="server" attribute then it works.
    So why is it that this attribute stops the document.getElementById from working and how do I get around it??

  5. #5
    Join Date
    May 2007
    Location
    Surrey, UK
    Posts
    75

    Solution

    just to let you know so that nobody wastes their time with this problem, I have found the solution.

    The problem was I was getting confused with the asp.net property visible="false" and the css property visibility:hidden; by adding visible="false" to the control asp never rendered it to the page so the element simply wasn't there to find with getElementById() now that I have removed visible="false" and added visibility:hidden; to a stylesheet it works perfectly!

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



Recent Articles