How to evaluate and generate a result from a html form using javascript -


I need to create a game when I say it in the form of title but my problem is that I do not know how the radio button Read Javascript and based on the options, it creates a scenario with the difficulty of choosing a player with the game mode. I am using a text input for the nickname and thus using 2 fields for the player to choose the type of game and difficulty. & lt; Fieldset & gt; & Lt; Story & gt; Dificuldade: & lt; / Narrative & gt; & Lt; Input type = "radio" name = "dificuldade" value = "easy" & gt; Easy & lt; / Input & gt; & Lt; Input type = "radio" name = "dificuldade" value = "medium" & gt; Medium & lt; / Input & gt; & Lt; Input type = "radio" name = "dificuldade" value = "hard" & gt; Tough & lt; / Input & gt; & Lt; / Fieldset & gt;

I suggest you use jQuery, making life so easy Goes:

  $ ('[name = dificuldade]: checked'). JSFiddle:  

Otherwise you will have to go through each of the radio buttons in the DOM, and check your check property When you get a checked === true , you can read its value property like this:

  Var fieldset = document.getElementById ('difficuldade'); // You must set an id for Fields element var curr = fieldset.firstChild; While (curr! = Null) {if (curr.checked) {break; } Curr = curr.next sibling; } Curr.value; // This is your selected value   

JSFiddle:

As Nate is mentioned, make sure Dome is ready, otherwise it will not work. This means that all your code should be run on the onload on the body element. For more details see:

Comments

Popular posts from this blog

java - NullPointerException for a 2d Array -

python - Assemble mpeg file unable to play in mediaplayer -

c# - NameSpace Manager or XsltContent to parse aspx page -