View Full Version : Javascript Variable function definition?

07-02-2011, 09:47 PM
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?

<script type="text/javascript">
var xmlDoc=xmlhttp.responseXML;
var x=xmlDoc.getElementsByTagName("entry");
var idVar;
for (var i=0;i<x.length;i++)
document.write("<button class='headerBar' onclick='showInfo(idVar)'><strong>");
document.write("<span id='x' onclick='hideInfo(this.id)' style='display:inline;'>");

Thanks in advance to anyone that can help with this.

07-03-2011, 07:02 PM
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.

07-03-2011, 07:17 PM
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.