/    Sign up×
Community /Pin to ProfileBookmark

array in javascript

Hi all,

I have an array named numbers declared as global and set in a function. I would like to use that array in a javascript to put a number into an <input…
field.

I can’t figure out how to reference that array in the javascript. Is there a way?

<script language=”javascript” type=”text/javascript”>
function shownumber(sel)
{
alert(“in shownumber, sel=”+sel);
alert(document.My_form.$numbers[sel].value);
document.My_form.numbers.value=document.My_form.$numbers[sel].value;
}
</script>

Error Console:
Error: document.My_form.$numbers has no properties

TIA,
DD

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@Banana_AnandaJun 28.2007 — I have an array named numbers declared as global [/QUOTE]

[CODE]document.My_form.numbers.value[/CODE]

Then why are you treating it like a form control ?
Copy linkTweet thisAlerts:
@DickDeedsauthorJun 28.2007 — Let me change the question from "I can't figure out how to reference that array in the javascript. Is there a way?"

to

"How can I reference the array in javascript or does javascript not allow it?"

DD
Copy linkTweet thisAlerts:
@Banana_AnandaJun 28.2007 — I'm confused, because I can't see an array anywhere. Are you sure that we don't have some problem with terminology. Do you mean "select box" perhaps ?

Would it be possible to post a more complete sample, stripped down, but including the relevant HTML ?
Copy linkTweet thisAlerts:
@DickDeedsauthorJun 28.2007 — Thanks for the reply. Here's the relavent code:

This is in php portion

$sel is a number;

$numbers=array();

//// start the HTML

<form onsubmit="return checkform(this);" name="my_form" action="index.php?' . strip_tags (SID) . '&t=Church:Collection" method="post">


………………

<!-- INPUT TOTAL AMOUNT - MUST HAVE AN AMOUNT IN ORDER TO SUBMIT -->

………….

<td class=back2>

input type="text" id="amount" name="amount" onfocus="shownumber($sel)"; onblur="checkinput()"; value='.$amount.'>

</td>

<td class=back2>

<input type="text" id="number" disabled name="number" value='.$number.'>

</td>

</form>

///// end of HTML

…………..

<script language="javascript" type="text/javascript">

function shownumber(sel)

{

alert("in shownumber, sel="+sel);

alert(document.my_form.$numbers[sel]);

}

</script>

Hope this helps. I'm new to javascript, obviously, and could not find any solution in my manuals.

sel is the postion in the array that I'm trying to display.

TIA,

DD
Copy linkTweet thisAlerts:
@Banana_AnandaJun 28.2007 — No can do. The array is a PHP array on the server. The client-side script has no awareness of its existence, if indeed it does still exist. You will have to serialize the PHP array into Javascript.
Copy linkTweet thisAlerts:
@DickDeedsauthorJun 28.2007 — Thanks again. How does one go about serializing a PHP array into javascript?

DD
×

Success!

Help @DickDeeds spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...