PDA

View Full Version : Basic javascript help


Rizon
03-08-2011, 10:18 PM
I'm trying to get this code to output first and last name into the 3rd box. It's not working and I'm not sure why. Any help?

*I can't figure out how to put code in here, so I just attached the file.

3ric
03-09-2011, 04:28 AM
I'm not seeing any attachment?

Ronnie Dobbs2
03-09-2011, 06:31 AM
I think it's (bracket)code(bracket)CODE GOES HERE(bracket)/code(bracket).

Replace the (bracket) with [ and ] obv.

But yeah, nothing attached on my end either.

Rizon
03-09-2011, 10:02 AM
Oops. Here it is

<html>
<head>
<title>
COMSC 100 WorkbookAssignment 4
</title>
<script>
function GO()
{

// declare a variable
var firstName;

// get variable's value
firstName = document.getElementById("firstName").value;

// insert the value of firstName
insert(firstName)

// declare a variable
var lastName;

// get variable's value
lastName = document.getElementById("lastName").value;

// insert the value of lastName
insert(lastName)

// declare a variable
var fullName;

// get variable's value
fullName = firstName.value + " " + lastName;
}
</script>

</head>
<body>
Instructions: <br>
Type your first and last name, then click GO<br>
and a greeting will appear.<br>
<br>
Input values:<br>

First Name: <input id="firstName"><br>
Last Name: <input id="lastName"><br>
<input type="submit" value="GO" onclick="GO()"><br>
<br>
Output value:<br>
Happy New Year, <input type="fullName"><br>

</body>
</html>

Rizon
03-09-2011, 10:03 AM
Trying to get it to look like this, where the you enter your first and last name, hit GO and it puts your full name in the last box.

<html>
<head>
<title>
COMSC 100 WorkbookAssignment 4
</title>
<script>
function GO()
{

// declare a variable
var firstName;

// get variable's value
firstName = document.getElementById("firstName").value;

// insert the value of firstName
insert(firstName)

// declare a variable
var lastName;

// get variable's value
lastName = document.getElementById("lastName").value;

// insert the value of lastName
insert(lastName)

// declare a variable
var fullName;

// get variable's value
fullName = firstName.value + " " + lastName;
}
</script>

</head>
<body>
Instructions: <br>
Type your first and last name, then click GO<br>
and a greeting will appear.<br>
<br>
Input values:<br>

First Name: <input id="firstName"><br>
Last Name: <input id="lastName"><br>
<input type="submit" value="GO" onclick="GO()"><br>
<br>
Output value:<br>
Happy New Year, <input type="fullName"><br>

</body>
</html>

Coffee Warlord
03-09-2011, 10:26 AM
function popFull()
{
form1.fullname.value = form1.firstName.value + " " + form1.lastName.value;
}


That's all you need. Call popFull as an onClick in the 'go' button, done.


<form name='form1'>
<input type='text' name='firstName'>
<input type='text' name='lastName'>

<input type='text' name='fullname'>

<input type='button' value='Go' onClick="popFull();">

Rizon
03-09-2011, 12:23 PM
Thanks CW. I'm totally code illiterate, so I'm not sure what to replace to the old code or where to add your code or both? We're supposed to be learning this in class, but the teacher gives the same story about cake and we don't seem to be learning any JS or HTML.

Coffee Warlord
03-09-2011, 12:32 PM
What I gave you is basically the completed everything. You only need that one line of javascript to accomplish what you want, and I included the actual necessary portion of the html. You'll need to add like the html/body/etc tags and such, but the meat is all there.

Ronnie Dobbs2
03-09-2011, 03:23 PM
If you wanted to do it with your original code (which might be more appropriate for a CSCI 1 assignment) I would ask you to look at the insert function. You are saying WHAT to insert, but not including WHERE.
http://www.prototypejs.org/api/element/insert

Coffee Warlord
03-09-2011, 03:50 PM
Learn it the efficient way, dammit. Don't learn bloated programming techniques. :)