Jump to content
  • Advertisement
Sign in to follow this  
namingway

[web] JavaScript help

This topic is 3773 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

When I undertook my multimedia exam last year, for the coding questions they let you select your prefered language with radio buttons and the textarea, chagned the code accordingly, I liked this idea as you could see the same code written in a few different languages, I'd like to try this out with javascript on a webpage, but I dont really know where to store the different code to display, At first thought I'm thinking that a switch statement or if/else block to check which radio button is selected and then it will display the text. So my question is what would be the best way of doing this? -regards Namingway

Share this post


Link to post
Share on other sites
Advertisement
If I were you'd, i'd approach this by storing the code in several different hidden DIV containers. You can then use javascript to listen for which radio button is active, and turn the appropriate div from hidden to visable.

I have similar code I wrote not to long ago here:
http://www.lordsofmidnight.com/test.php

If you mouse over the ?'s on the left, you'll see how the div fields not only go from hidden to non-hidden, but are placed on the page based on the location of the image file. I was pretty proud of writing it.

Share this post


Link to post
Share on other sites
Quote:
Original post by namingway
how do you make hidden div containers?

display: none;

To make them visible, you set display: block;

Share this post


Link to post
Share on other sites
Feel free to do a 'view source' on my code and steal from it as you like. If you have any questions about it, just ask.

Share this post


Link to post
Share on other sites
okay here's the basic setup before I actually make the code to print out to the textarea, it isnt working as of yet :( I'm not quite sure what to fix up to get this working.

<script type="text/javascript">
<!--
function ShowLanguageCode(lang)
{
if(lang == "C")
{
return document.getElementById("txt").value = "This is the C code";
}
else
{
return document.getElementById("txt").value = "No code";
}
}
-->
</script>

</head>

<body>
Select your language:
<form name="testForm">
<input type="radio" name="language" value="C" onclick="ShowLanguageCode("C")"/>C<br />
<input type="radio" name="language" value="Cplusplus" />C++<br />
<input type="radio" name="language" value="Csharp" />C#<br />
<input type="radio" name="language" value="Delphi" />Delphi<br />
<input type="radio" name="language" value="Java" />Java<br />
<input type="radio" name="language" value="VB" />Visual Basic<br />
<textarea id="txt"></textarea>
</form>
</body>




My goal was to have it so if you click the C radio button it displays the text in textArea: This is the C code

Share this post


Link to post
Share on other sites
When you pass in 'C', use single quotes in lieu of doubles. Ie:

replace:

<input type="radio" name="language" value="C" onclick="ShowLanguageCode("C")"/>C<br />

with

<input type="radio" name="language" value="C" onclick="ShowLanguageCode('C')"/>C<br />

Share this post


Link to post
Share on other sites
Is it at all possible to get the text to appear in a table or a CSS made box instead of the textbox, that way I can have formated text.

Share this post


Link to post
Share on other sites
You can do this with the use of DIV's. Basically, you'll do the following:

<div id = 1 position = 'xx', hidden>Code for C#</div>
<div id = 2 position = 'xx', hidden>Code for C++</div>
...
<div id = 10 position = 'xx', hidden>Code for Java</div>

Since they are all hidden, they will not show up unless they are set to visible. The code would be very similar to what you have now, except you'd change the visible/hidden attribute on each div as you onclicked and offclicked each radio button.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!