Log in

View Full Version : Element help



NitroDev
11-11-2013, 02:22 PM
Hi! I want to get these input names:


function charCreate()
{
document.body.innerHTML="<pre><h3>Start by choosing your name!<\/h3><br><br><input type='text' name='name'><br><br><input type='radio' name='gender' value='Male'>Male<br><input type='radio' name='gender' value='Female'>Female<br><br><button onClick='newWorld()'>New World<\/button><pre>"
}

to here:


function characterProfile()
{
document.body.innerHTML="<pre><h2>Character Profile<\/h2><\/pre>"
document.body.innerHTML+="<pre><p>Gender: <\/p><\/pre>"
}

every answer is welcome!

traq
11-12-2013, 01:17 AM
Do you mean you want the value of the checked button?


function getGenderFromRadio(){
var radio_gender = document.getElementsByName( 'gender' );
for( var i=0; i<radio_gender.length; i++ ){
if( radio_gender[i].checked ){
return radio_gender[i].value;
}
}
return 'unknown';
}

NitroDev
11-12-2013, 12:27 PM
Yes but in my game code that doesn't seem to work


<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>&nbsp;</title>

<script>
function characterProfile()
{
var radio_gender = document.getElementsByName( 'gender' );
for( var i=0; i<radio_gender.length; i++ ){
if( radio_gender[i].checked ){
return radio_gender[i].value;
}
}
return 'unknown';
}
function credit()
{
document.body.innerHTML+="<pre><h3>These people helped me alot:<\/h3><br><br><a href='http://www.dynamicdrive.com/forums/'>Dynamic Drive<\/a><\/pre>"
}
function craft()
{
document.body.innerHTML="<pre><p>What do you want to craft?<\/p><button onClick='newWorld()'>Back<\/button><\/pre>"
}
function explore()
{
document.body.innerHTML="<pre><h1>Where do you want to explore?<\/h1><button onClick='newWorld()'>Back<\/button><br><button onClick='exploreNorth()'>North<\/button><\/pre>"
}

<!--Below are the explore() functions
function exploreNorth()
{
var found='a branch'
document.body.innerHTML="<pre><h4>You walk towards north and find "+found+"<\/h4><br><button onClick='explore()'>Back<\/button><\/pre>"
}
function myInventory1()
{
document.body.innerHTML="<pre>You came from first inventory item <button onClick='newWorld()'>Back<\/button><\/pre>"
}

function myInventory2()
{
document.body.innerHTML="<pre>You came from second inventory item <button onClick='newWorld()'>Back<\/button><\/pre>"
}

function myInventory3()
{
document.body.innerHTML="<pre>You came from third inventory item <button onClick='newWorld()'>Back<\/button><\/pre>"
}

function showInventory()
{
document.getElementById('inventory').style.display='block';
document.getElementById('hide_list').style.display='inline'
}

function hideInventory()
{
document.getElementById('inventory').style.display='none';
document.getElementById('hide_list').style.display='none'
}


function newWorld()
{
document.body.innerHTML="<pre><h1>Hello and welcome to World of My Core!<\/h1>What would you like to do?<br><button style='width:145px' onClick='window.location.reload()'>Exit<\/button><br><button onClick='showInventory()' style='display: inline; width: 145px'>Show Inventory Items<\/button>&nbsp;<button id='hide_list' style='display: none' onclick='hideInventory()'>Hide Inventory<\/button><ul id='inventory' style='margin-top: 10px; margin-bottom: 0px; display: none'><li onclick='myInventory1()'>first item<\/li><li onclick='myInventory2()'>second item<\/li><li onclick='myInventory3()'>third item<\/li><\/ul><br><button onClick='craft()' style='width:145px'>Craft<\/button><br><button onClick='explore()' style='width:145px'>Explore<\/button><br><button style='width:145px' onClick='characterProfile()'>Profile<\/button><\/pre>"
}
function charCreate()
{
document.body.innerHTML="<pre><h3>Start by choosing your name!<\/h3><br><br><input type='text' name='name'><br><br><input type='radio' name='gender' value='Male'>Male<br><input type='radio' name='gender' value='Female'>Female<br><br><button onClick='newWorld()'>New World<\/button><pre>"
}
</script>

<style>
pre{font-family: verdana; font-size: 12px}
button {font-family: arial; font-size: 13px}
button,li{cursor: pointer}
</style>
</head>

<h1 style="color:brown">MY CORE</h1>


<button onClick="charCreate()">Play</button><br>
<button onClick="credit()">Credits</button>

</body>
</html>

traq
11-12-2013, 07:55 PM
I… don't see where you're trying to use it. You seem to have replaced your entire characterProfile function with the contents of my getGenderFromRadio function.

example usage (http://jsfiddle.net/traq/9my2g/4/)

NitroDev
11-13-2013, 11:37 AM
Correct so what should i do then? Like this?


function characterProfile()
{
var radio_gender = document.getElementsByName( 'gender' );
for( var i=0; i<radio_gender.length; i++ ){
if( radio_gender[i].checked ){
return radio_gender[i].value;
}
}
document.body.innerHTML+="<pre><p>Gender: "+radio_gender+"<\/p><\/pre>
}

traq
11-14-2013, 01:50 AM
No, you shouldn't be replacing your original function at all. Simply add a call to the new function at the spot you want the gender term inserted.

Look at, and experiment with, the working example (http://jsfiddle.net/traq/9my2g/4/) I offered.

NitroDev
11-14-2013, 03:28 PM
umm like this?


function characterProfile(){
{
document.body.innerHTML="<pre><h2>Profile<\/h2><\/pre>"
var radio_gender = document.getElementsByName( 'gender' );
for( var i=0; i<radio_gender.length; i++ ){
if( radio_gender[i].checked ){
return radio_gender[i].value;
}
}
return 'unknown';

document.body.innerHTML+="<pre><p>Gender: "+radio_gender+"<\/p><\/pre>"
}

traq
11-14-2013, 07:52 PM
Did you try that? Did it do what you wanted?

Did you look at the working example I set up for you?

NitroDev
11-15-2013, 05:33 PM
Yes but i wanted to NOT have the button just to instantly show that Gender: Male/Female

traq
11-15-2013, 05:58 PM
That's fine — you don't have to. It's an example so you could see how to integrate the getGender... function with your existing createProfile function. You can call the createProfile function however you like, it's up to you. Look at the code before dismissing it. Almost all of your code could remain unchanged —do you see where I added the call to my new function, at the bottom of your existing function? That's all there is to it.


var characterProfile = function(){
p.innerHTML="<pre><h2>Character Profile<\/h2><\/pre>"
+ "<pre><p>Gender: "
+ getGenderFromRadio()
+ "<\/p><\/pre>";
}

NitroDev
11-16-2013, 09:20 AM
Okay but i cant find the example anymore

traq
11-16-2013, 03:25 PM
...that's really weird. The links were working yesterday. I fixed them. Here's the correct URL: http://jsfiddle.net/traq/9my2g/4/

NitroDev
11-16-2013, 05:51 PM
okay i worked it out now but anyway heres the code so far:


<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>&nbsp;</title>

<script>
var b = document.getElementById( 'b' );
var p = document.getElementById( 'p' );

b.addEventListener( 'click',function(){
characterProfile();
});

function getGenderFromRadio(){
var radio_gender = document.getElementsByName( 'gender' );
for( var i=0; i<radio_gender.length; i++ ){
if( radio_gender[i].checked ){
return radio_gender[i].value;
}
}
return 'unknown';
}
function characterProfile()
p.innerHTML="<pre><h2>Character Profile<\/h2><\/pre>"
+ "<pre><p>Gender: "
+ getGenderFromRadio()
+ "<\/p><\/pre>";
}

function credit()
{
document.body.innerHTML+="<pre><h3>These people helped me alot:<\/h3><br><br><a href='http://www.dynamicdrive.com/forums/'>Dynamic Drive<\/a><\/pre>"
}
function craft()
{
document.body.innerHTML="<pre><p>What do you want to craft?<\/p><button onClick='newWorld()'>Back<\/button><\/pre>"
}
function explore()
{
document.body.innerHTML="<pre><h1>Where do you want to explore?<\/h1><button onClick='newWorld()'>Back<\/button><br><button onClick='exploreNorth()'>North<\/button><\/pre>"
}

<!--Below are the explore() functions
function exploreNorth()
{
var found='a branch'
document.body.innerHTML="<pre><h4>You walk towards north and find "+found+"<\/h4><br><button onClick='explore()'>Back<\/button><\/pre>"
}
function myInventory1()
{
document.body.innerHTML="<pre>You came from first inventory item <button onClick='newWorld()'>Back<\/button><\/pre>"
}

function myInventory2()
{
document.body.innerHTML="<pre>You came from second inventory item <button onClick='newWorld()'>Back<\/button><\/pre>"
}

function myInventory3()
{
document.body.innerHTML="<pre>You came from third inventory item <button onClick='newWorld()'>Back<\/button><\/pre>"
}

function showInventory()
{
document.getElementById('inventory').style.display='block';
document.getElementById('hide_list').style.display='inline'
}

function hideInventory()
{
document.getElementById('inventory').style.display='none';
document.getElementById('hide_list').style.display='none'
}


function newWorld()
{
document.body.innerHTML="<pre><h1>Hello and welcome to World of My Core!<\/h1>What would you like to do?<br><button style='width:145px' onClick='window.location.reload()'>Exit<\/button><br><button onClick='showInventory()' style='display: inline; width: 145px'>Show Inventory Items<\/button>&nbsp;<button id='hide_list' style='display: none' onclick='hideInventory()'>Hide Inventory<\/button><ul id='inventory' style='margin-top: 10px; margin-bottom: 0px; display: none'><li onclick='myInventory1()'>first item<\/li><li onclick='myInventory2()'>second item<\/li><li onclick='myInventory3()'>third item<\/li><\/ul><br><button onClick='craft()' style='width:145px'>Craft<\/button><br><button onClick='explore()' style='width:145px'>Explore<\/button><br><button style='width:145px' onClick='characterProfile()'>Profile<\/button><\/pre>"
}
function charCreate()
{
document.body.innerHTML="<pre><h3>Start by choosing your name!<\/h3><br><br><input type='text' name='name'><br><br><input type='radio' name='gender' value='Male'>Male<br><input type='radio' name='gender' value='Female'>Female<br><br><button onClick='newWorld()'>New World<\/button><pre>"
}
</script>

<style>
pre{font-family: verdana; font-size: 12px}
button {font-family: arial; font-size: 13px}
button,li{cursor: pointer}
</style>
</head>

<h1 style="color:brown">MY CORE</h1>


<button onClick="charCreate()">Play</button><br>
<button onClick="credit()">Credits</button>

</body>
</html>

traq
11-16-2013, 11:15 PM
cool. Is it working the way you want?

If your question has been answered, please mark your thread "resolved":
On your original post (post #1), click [edit], then click [go advanced]. In the "thread prefix" box, select "Resolved". Click [save changes].

NitroDev
11-17-2013, 08:01 AM
No it wont go past the "start menu"

traq
11-17-2013, 08:00 PM
Are you testing your code? I would highly recommend using a console like Firebug or Chrome's Dev Tools. This makes it easy to find simple problems - the console will literally tell you what, and where, the problem is. In this case, there's an uncaught syntax error in your script, which causes it to stop execution (which is why nothing seems to happen). When you wrote the characterProfile function, you left out the opening curly brace:
function characterProfile()
p.innerHTML="<pre><h2>Character Profile<\/h2><\/pre>"
// . . .

--- should be ---

function characterProfile(){
p.innerHTML="<pre><h2>Character Profile<\/h2><\/pre>"
// . . .
There are code editors that do the same things—syntax highlighting, error checking, autocompletion, code reference, and much much more.
Komodo Edit, for example (my favorite), supports quite a few different languages, works on Linux, Mac, or Windows, and is free (as in beer).

After you fix that error, your script seems to work, up to the point where you click on the [Profile] button. Look at the console, and you see this:
Uncaught TypeError: Cannot set property 'innerHTML' of null
Think about this:

[1] When you click the [Profile] button, it is supposed to run the characterProfile function.

[2] The characterProfile function does try to use the innerHTML property.

[3] Let's assume, for the moment, that this is where the problem is. If so, that means that p must be null.

[4] We defined p up at the top of the script: var p = document.getElementById( 'p' );.
If p is null, that means that getElementById couldn't find an element on the page with id=p (and so returned null instead).

[5] Is there an element on the page with an id of "p"? Nope. There's our problem.
In the example I showed you, I had a paragraph with id=p that I was using to hold the result of the function, but you have no such paragraph in your markup.

There's another problem, after this: can you figure out what it is?

NitroDev
11-18-2013, 05:44 AM
Okay thanks alot now i have been using the JavaScript console and this keeps coming up


Uncaught TypeError: Cannot set property 'innerHTML' of null TestMyCore.html:25
characterProfile TestMyCore.html:25
onclick

I have looked all around the script and now i have added the id for p but i dont understand where i should add the id for b

traq
11-18-2013, 06:10 AM
Okay thanks alot now i have been using the JavaScript console and this keeps coming up

Uncaught TypeError: Cannot set property 'innerHTML' of null TestMyCore.html:25
I have looked all around the script and now i have added the id for p but i dont understand where i should add the id for b

As you can see, adding an element with the id of "p" won't actually solve this — the problem I alluded to above is that, even if you add an element with the proper id (e.g., <p id=p></p>), it would be gone by the time you reach the point you need it because your previous functions overwrite the entire document body. You're erasing and replacing all of the page content on each button click. One possible solution (not the best, though, as you'll see below) would be to take the same approach as your other functions. Instead of trying to do p.innerHTML, do document.body.innerHTML again.

As far as b goes, that will require a little more planning. Any "#b" element you put on the page will also be overwritten by the time you call this function.

However, it's harder to say what to do because your "male" and "female" buttons are shown on the step before you call createProfile. You show the "Start by choosing your name!" screen, along with the M / F radios and a button that says [New World]. This button doesn't call the createProfile function, though, it calls the newWorld function. The newWorld function calls createProfile, but the radio buttons are already overwritten at this point, so there is no way to get their value: they're gone.

I would suggest taking a step back and re-thinking your program flow: go back to your design stage and plan out which screens need to come in which order, what resources/information each step will need from the previous steps, and how you plan to pass those resources between "screens."

How familiar are you with javascript? For example, do you know how to pass arguments to your functions? do you know how to create objects with methods? do you know what a controller is?

Your current approach of rewriting the entire body for each new screen will quickly become very difficult to manage as your program becomes more complex.

NitroDev
11-18-2013, 02:02 PM
Okay i really dont know what to say to that traq because molendjk "teached" on one post the
document.body.innerHTML so im not familiar with anything else and i have no idea what "pass arguments to your functions" means so im a complete noob at JavaScript

traq
11-19-2013, 03:12 AM
Okay i really dont know what to say to that traq because molendjk "teached" on one post the
document.body.innerHTML
That's fine - the problem isn't that you're using the innerHTML property, it's just that you're getting into a situation where you have to carefully plan what you replace and when.


i have no idea what "pass arguments to your functions" means so im a complete noob at JavaScript
I would suggest some basic javascript tutorials, then.

Functions can accept arguments when you call them. For example, with this function:
var hello = function( name ){
var greeting = "Hello, " + name + "!"
return greeting;
}

name is an argument. I can call the function, and pass whatever value I like as that argument, and the function will use it (internally) with the name name:
var helloWorld = hello( 'World' );

Now, the function runs, using the value "World" for the name variable, and returns the new greeting to me. Not all functions have arguments, but most do, and it can make them a a lot more useful.

For now, I would highly recommend that you sit down with some notecards and do some design work:
outline each "screen" your program will need.
go back through them and decide what information each screen will need to work properly.
figure out where that information needs to come from.
figure out how you're going to get it.

NitroDev
11-19-2013, 02:03 PM
Thanks traq and now i have a question is it possible to add multiple
Values on same variable on same function

traq
11-19-2013, 05:40 PM
Thanks traq and now i have a question is it possible to add multiple
Values on same variable on same function

I'm not quite sure what you mean by that.

You can provide a new value each time you call a function:
var hello = function( name ){
var greeting = "Hello, " + name + "!";
return greeting;
}

hello( 'World' ); // Hello, World!
hello( 'Adrian' ); // Hello, Adrian!
hello( 'NitroDev' ); // Hello, NitroDev!

Functions can also have more than one argument:
var add = function( num1, num2 ){
var sum = num1 + num2;
return sum;
}

add( 50,-8 ); // 42

Functions can even have unnamed arguments (though this concept is a little more advanced, and is not always as useful as it seems):
var mystery = function(){
var count = arguments.length;
var response = "You passed " + count + " arguments to this function.";
return response;
}

mystery( 'one arg' ); // You passed 1 arguments to this function.
mystery( 'a','b','c' ); // You passed 3 arguments to this function.

If you're asking something else, please explain further.

NitroDev
11-21-2013, 09:12 AM
Okay i mean this


var example = hello, world

So one variable stores multiple values

traq
11-22-2013, 12:57 AM
Not exactly. You can use an array to store a list of values:
var myArray = [ 'hello', 'world' ];

console.log( myArray[0] ); // hello
console.log( myArray[1] ); // world

Or, you can use a plain object to store a map of keys and values:
var myObject = { me: "Tarzan", you: "Jane" };

console.log( myObject.me ); // Tarzan
console.log( myObject.you ); // Jane