javascript arrays

Q

question.boy

This is a begginner question, but I need to pass 1 or more value to a
javascript function from an onclick event on my form, could someone
help me get a functional example.

For my learning purposes, the example is quite simple. I would like
to have one button return a alert window for one passed value and
another button (using the same function) return 2 passed values. I'm
looking for a function that can handle single and multiple passed
variables.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://
www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1" />
<script language="javascript">
var aCtls = New Array();

function AlCtls(aCtls){
for (i=0;aCtls.length;i++) {
alert aCtls;
}
}

</script>
</head>

<body>
<input id="btn2" type="button" value="Submit"
onclick="AlCtls('1,2')"/>
</form>
</body>
</html>

Thank you for the help!

QB
 
Q

question.boy

I managed to get the following to work. Is there a better approach?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://
www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1" />
<script language="javascript">
function AlCtls(txtCtls){
var aCtls = txtCtls.split(',');
alert(aCtls.length);
for (i=0;i<=aCtls.length-1;i++) {
alert(aCtls);
}
}

</script>
</head>

<body>
<input id="btn2" type="button" value="Submit"
onclick="AlCtls('1,2');"/>
</form>
</body>
</html>

Thank you,

QB
 
V

VK

This is a begginner question, but I need to pass 1 or more value to a
javascript function from an onclick event on my form, could someone
help me get a functional example.

For my learning purposes, the example is quite simple. I would like
to have one button return a alert window for one passed value and
another button (using the same function) return 2 passed values. I'm
looking for a function that can handle single and multiple passed
variables.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1" />
<script language="javascript">
var aCtls = New Array();

function AlCtls(aCtls){
for (i=0;aCtls.length;i++) {
alert aCtls;
}

}

</script>
</head>

<body>
<form>
<input id="btn1" type="button" value="Submit" onclick="AlCtls('1')"/><br/>

<input id="btn2" type="button" value="Submit"
onclick="AlCtls('1,2')"/>
</form>
</body>
</html>

Thank you for the help!

QB


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html lang="en-US">
<head>
<meta http-equiv="Content-type"
content="text/html; charset=iso-8859-1">
<title>Demo</title>
<script type="text/javascript">
function multiArgs() {
for (var i=0;i<arguments.length;i++) {
window.alert(arguments);
}
}
</script>
</head>
<body>
<h1>Demo</h1>
<button type="button"
onclick="multiArgs(1)">One argument</button>
<button type="button"
onclick="multiArgs(1,2)">Two arguments</button>
<button type="button"
onclick="multiArgs(1,2,'OK')">Three arguments</button>
</body>
</html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top