Note that there are some explanatory texts on larger screens.

plurals
  1. POAuto submit form and retrieve form data using javascript/jquery
    primarykey
    data
    text
    <p>I'm looking to create a form that uses radio buttons but as soon as a radio button is clicked it runs a JavaScript function that retrieves data from the form inducing hidden values.</p> <p>I was thinking it should be something like this but I've had no luck. </p> <pre><code>&lt;script type="text/javascript"&gt; $(function submitFunction(formname) { $(".submit").onclick(function(formname) { var hiddenValue = document.formname.hidden_value.value; var vote = document.formname.vote.value; var dataString = 'vote=' + vote + 'hiddenValue=' + hiddenValue; alert(dataString); }); }); &lt;/script&gt; &lt;form action="post" name="form1" &gt; &lt;input type=hidden name='hidden_value' value='12345'&gt; &lt;input type='radio' name='vote' value='yes' onclick='submitFunction(form1)';&gt;Yes &lt;br/&gt; &lt;input type='radio' name='vote' value='no' onclick='submitFunction(form1)';&gt;No &lt;/form&gt; </code></pre> <p><strong>Edit:</strong> After some help from a reply on here I have some new code. It worked perfectly on jsFiddle but no luck when I implemented it myself. I made sure that jQuery is loading properly and it is version 1.51. Here is the code. Thanks in advance again.</p> <p>JavaScript: </p> <pre><code> if(jQuery){ alert("jQuery Here"); } $("input.liveRadio1").click(function() { var jThis = $(this); var hiddenValue = jThis.siblings('input[name="hidden_value"]').val(); var vote = jThis.val(); var dataString = 'vote=' + vote + 'hiddenValue=' + hiddenValue; alert(dataString); }); </code></pre> <p>HTML: </p> <pre><code> &lt;form action="post" name="form1" &gt; &lt;input type=hidden name='hidden_value' value='12345' /&gt; &lt;input class="liveRadio1" type='radio' name='vote' value='yes' /&gt;Yes &lt;br/&gt; &lt;input class="liveRadio1" type='radio' name='vote' value='no' /&gt;No &lt;/form&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload