I imagine this is quite a simple variable problem!

Discussion in 'JavaScript' started by dbarker348, Oct 24, 2007.

  1. #1
    Hey there,
    Hate that my first post is to ask for help but this little thing has been driving me crazy today! I've only just started getting into javascript so I imagine this would be quite simple to solve but I don't have a clue! :confused:

    Basically, I'm trying to use AJAX, but my problem lies with a simple variable. Basically, I'm trying to pass an ID number through the script so that I can chose with div to change for the AJAX script (I have lots of different DIVs on one page but want them just to use one script).

    This is the part of the code I'm having a problem with:
    Now, I imagined that whatever the value of 'divrating' would be, would be the Id used in document.getElementById(), but for some reason, it's taking it literally, and I'm getting the error "...getElementById(divrating)... has no properties". Whereas I want it to be using "...getElementById(TEST)...", in the above example.

    I want to be able to say divrating=1 or divrating=2 etc etc. But it just keeps using the variable name instead of the value of the variable no matter what I'm doing. I've tried putting single and double quotes around it but no change. :(

    I'd appreciate any input! Am I just being stupid?! lol

    Thanks so much,

    Dave
     
    dbarker348, Oct 24, 2007 IP
  2. Mike H.

    Mike H. Peon

    Messages:
    219
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I doubt that the code you posted is the actual code you are using.

    I'll bet that the variable divrating is declared in another function.

    And, "TEST" should never be used as an ID, and an ID should never be just a number, and it cannot begin with a number. Use n1, or something similar. "Test" is a JavaScript reserved word, and IDs that are only numbers are invalid HTML.

    Fix those errors, then post more code.
     
    Mike H., Oct 24, 2007 IP
  3. dbarker348

    dbarker348 Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for the swift reply. As far as I can tell divrating wasn't used elsewhere but to be sure I changed it. The other values I was just using as an example to illustrate my point. My actualy DIV ids are like "ratingdiv1", "ratingdiv2" etc. The code I now have is:

    However, it's still attempting to find the DIV with the id 'divratingbrandnew', not 'example100'.

    Just for completeness, this is the whole code of my js file:

    Any further ideas?

    Thanks,

    Dave
     
    dbarker348, Oct 24, 2007 IP
  4. Mike H.

    Mike H. Peon

    Messages:
    219
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Why is none of it indented? That is very difficult to read.

    You didn't post your HTML. Half the code is the same as none of it.

    This is wrong:

    var linkid;

    function ratingup(linkid) {

    passing linkid to the function does not pass the variable linkid to it.




    Take a look at my code here:
    http://ajaxforums.net/index.php?topic=871.0
     
    Mike H., Oct 24, 2007 IP
  5. dbarker348

    dbarker348 Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I appreciate your response, however, my HTML code is all done dynamically through PHP and is 500+ lines long. My only issue is why
    is looking for the DIV 'divratingbrandnew' instead of for the DIV 'example100'. Maybe I didn't make myself clear enough. Why is it not replacing divratingbrandnew with the value for the variable of divratingbrandnew?

    Thanks,
    Dave
     
    dbarker348, Oct 24, 2007 IP
  6. Mike H.

    Mike H. Peon

    Messages:
    219
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Dave:

    No, your question was clear, I should have stated that the original code you posted will work, but, the HTML must be the problem. And now that you tell me that it is built with PHP, I have no doubt that is the problem.

    Copy the following code, and save it as an HTML file, then open it.

    You'll see that it "works."

    Your problem is in the HTML. The ID that you think should exist, doesn't.
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
       "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Any Title</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <script type="text/javascript">
    
    	var divID = "n1";
    	
    	function init(){
    		
    		document.getElementById(divID).innerHTML = "Hello";
    	}
    
    	onload=init;
    	
    </script>
    <style type="text/css">
    
    	 body {background-color:#eae3c6;margin-top:60px}
    	
    </style>
    </head>
    	<body>
    		<div id="n1"></div>
    	</body>
    </html>
    
    Code (markup):
     
    Mike H., Oct 24, 2007 IP
  7. dbarker348

    dbarker348 Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Thanks so much for the reply. Because I was just using an example, I didn't realise that it would mess up the code if I didn't actually have a DIV in the HTML which was called the example I was using.Thanks for that.

    Well that mystery is sorted but I now have another question.:confused:
    My HTML/PHP is as follows:
    Where $id has numerical values of 1 through about 150 right now. So I'm wanting to make a DIV for each one: "linkrating1", "linkrating2" etc. That's working fine. It's just passing the $id through to the javascript that's causing problems. I'm not sure if my code is right or not.

    I added the alert() to see if the linkid was coming through properly, and when I click, it is showing the linkid variable correctly. However, I'm wondering if I need to pass the linkid variable through to the stateChanged function, as it's giving me the error that linkid is undefined on line 24. which is this one:
    I can't seem to find how to do that.

    Thanks again! :)
    Dave
     
    dbarker348, Oct 24, 2007 IP
  8. Mike H.

    Mike H. Peon

    Messages:
    219
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Dave:

    You need to make that variable "global" by declaring it outside of the functions.

    I like to set variables to an empty string.

    Note the boldface changes, and note that this: nLinkID = nID; is NOT preceded by var. The variable is declared outside the function, making it "global" so that it can be used by ANY function.
    
    [B]var xmlHttp = "";
    var nLinkID = "";[/B]
    
    function ratingup([B]nID[/B])
    
        {
       [B] nLinkID = nID;[/B]
        xmlHttp=GetXmlHttpObject();
        if (xmlHttp==null)
            {
            alert ("Your browser does not support AJAX!");
            return;
            } 
        var url="ajaxrating.php";
        url=url+"?id="+[B]nLinkID;[/B]
        url=url+"&sid="+Math.random();
        xmlHttp.onreadystatechange=stateChanged;
        xmlHttp.open("GET",url,true);
        xmlHttp.send(null);
        alert('linkid');
        }
    
    function stateChanged()
    
        {
        if (xmlHttp.readyState==4)
            {
            var divID = "linkrating"+[B]nLinkID[/B]
            document.getElementById(divID).innerHTML=xmlHttp.responseText;
            }
        }
    Code (markup):
     
    Mike H., Oct 25, 2007 IP
  9. dbarker348

    dbarker348 Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Thanks so much Mike! You're a star - works perfectly :D

    Thanks again,
    Dave
     
    dbarker348, Oct 25, 2007 IP
  10. Mike H.

    Mike H. Peon

    Messages:
    219
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #10
    You're welcome, Dave. Good luck with your project.
     
    Mike H., Oct 25, 2007 IP