PDA

View Full Version : Javascript: Using variables


Agent009
09-06-2008, 03:21 AM
Hi,

I have a simple question.

I have a funtion as such.

function update_stats(stat,initial,action){
original = document.charform.initial.value;

}

Where "initial" holds value for a hidden text field, i.e. "original". I want to get the value of this text field using the var "initial", but I can't seem to plug this variable into the statement above, because it instead looks for a field named "initial".

I know there are other ways of getting the required data, through getElementById as well as declaring the field through the function (e.g. this.form.initial), but I want to know if there is anyway I can tell the statement that initial is a variable, not the FIELD name I am looking for.

Any help will be greately appreciated.

Cheers.

rangana
09-06-2008, 04:24 AM
I would like to read through your function. This part:

original = document.charform.initial.value;


...is assigning the value of the input whose name is initial that is a child of a form element whose ID is charform to original variable.
I see nothing wrong on this, not unless you would show us your markup :agree:

Your function accepts three parameters:

function update_stats(stat,initial,action)


You said that initial variable has the value of that hidden field. Have you tried showing the value that the variable is holding :confused:

alert(initial);


I believe that it will return the value of the hidden field whose name is initial, and you can assign it directly to original variable.

If so, then your script should be:

function update_stats(stat,initial,action){
alert(initial) ; // Try to alert the value of initial variable.
original = initial;
}


Hope that helps.

If nothing worked, please show us the markup (HTML) involved.

Agent009
09-06-2008, 05:56 AM
Sorry, I might have mistyped the problem.

Initial does not hold the value of the hidden text field, it holds the name/id of it.

i.e. when calling the function, it will be:

update_stats('str',o_str,1);

Where the string o_str is declared as the initial variable.

What I wanted to find out was if there was anywhere I could tell the statement that when I type initial, I want it to treat it as a variable and get the relevant string, not treat it as a string itself.

i.e. initial = document.charform.initial(here, read the text field name from the initial variable, do not take the string initial to be the text field name).value;

I fear I'm doing a terrible job of explaining this...

rangana
09-06-2008, 06:38 AM
Why not just utilize the this keyword in replacement of highlighted:

update_stats('str',o_str,1);


I'm really having difficulty understanding this one. Could you please show us the involved markup (HTML)

Agent009
09-06-2008, 06:52 AM
Something like this.


<form name="charform" method="post" action="{POST}">
<table>
<tr>
<td><input type="hidden" name="o_str" value="10"><input type="button" name="i_str" value="+" onClick="update_stats('str',o_str,1);" /></td>
</tr>
</table>
</form>


And I don't really understand how "this" works in javascript.

rangana
09-06-2008, 07:22 AM
function update_stats(stat,initial,action){
var original = document.charform.elements[initial.name].value;
alert(original); // You can remove this part. Just for testing
}

Agent009
09-06-2008, 07:55 AM
I see. Very nice and tidy. Thanks a lot mate :)

rangana
09-06-2008, 09:07 AM
You're welcome :)