Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jul 2011
    Location
    Missouri
    Posts
    9
    Thanks
    3
    Thanked 1 Time in 1 Post

    Javascript Variable function definition?

    I am trying to create a page that uses javascript to call data from an xml file to present a large quantity of information through an for loop. I have gotten the id of the element to work appropriately, but using the funtion defines it as the last cycled variable definition. Is there a way to pass the value of the variable to the function definition, ie.. onclick="function(var)", so that it stays that value when the loop cycles back through?

    Code:
    <script type="text/javascript">
    	xmlhttp.open("GET","companyInfo.xml",false);
    	xmlhttp.send();
    	var xmlDoc=xmlhttp.responseXML;
    	var x=xmlDoc.getElementsByTagName("entry");
    	var idVar;
    	for (var i=0;i<x.length;i++)
    	{
    		idVar=x[i].getElementsByTagName("id")[0].childNodes[0].nodeValue;
    		document.write("<button class='headerBar' onclick='showInfo(idVar)'><strong>");
    		document.write(x[i].getElementsByTagName("heading")[0].childNodes[0].nodeValue);
    		document.write("<strong></button>");
    		document.write("<span id='x' onclick='hideInfo(this.id)' style='display:inline;'>");
    		document.getElementById("x").id=idVar;
    		document.write(x[i].getElementsByTagName("page1")[0].childNodes[0].nodeValue);
    		document.write("</span>");
    	}
    </script>
    Thanks in advance to anyone that can help with this.

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,354
    Thanks
    23
    Thanked 618 Times in 617 Posts
    First off Ollie, you don't have a function here. I'm betting you know how though:

    fucntion read_file(thing){

    put the code in here

    }

    and yes onclick="read_file(your_var)" will call it and pass the variable var to it. By the way, you can just use thing as the variable name inside the function.

    I'm from West Plains, MO. Where you from? Anywhere close? I feel like the Lone EnCoder out here.

  • #3
    New to the CF scene
    Join Date
    Jul 2011
    Location
    Missouri
    Posts
    9
    Thanks
    3
    Thanked 1 Time in 1 Post
    The function itself is defined in the head of the html. This loop defines the buttons that will call the function when clicked. Basically the function is designed to expand the information that is associated with the button clicked. The problem with hust using the variable itself as the argument is that the variable changes each time the loop cycles through to write the button, so when the info is expanded, all buttons open the last section of information. I am trying to automate this all through the script so that the only thing on the site that needs to be updated is the xml file. The same data comes through multiple pages and versions of the page, ie.. screen and mobile. The element id for the block of text to be expanded is defined in the xml and brought in, but I need that value placed into the function argument on the onclick.

    As for where in MO, I'm in Joplin.


  •  

    Posting Permissions

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