So I need to tell if a radio button is clicked. I’ve found many snippets online that show me how to use listeners or basically tell me when it’s changed. I just want to know after the page loads if this radio button is ‘checked’
Here is the HTML I’m working with…
I have no control over the HTML, content, or anything.
My end goal is to
test the state of #radioFedEthNo
and set a variable ‘Ethno’ to ‘H’ if it is ‘checked’
eventually use this information to change info later on in the page when the submit button is selected
But the first part of this is just me being able to figure out if this button is checked without waiting for an event. I found this on the nets and I think it’s VERY close to what I want, but it’s waiting for an event:
Can anyone help me change this to just check with .change() I just don’t know enough about jQuery to ask google the right question.
Thanks for any help.
just want to know after the page loads if this radio button is ‘checked’
When you say “after the page loads” it makes me think you want to check it immediately once the page has fully loaded, in which case you can simply do:
However, this seems like an unlikely scenario, as when this code is run, the user won’t have had a chance to change anything, so you know it’s not checked (presuming it started not checked).
Did you mean “after the page loads” (and thus before the user could have changed it)?
If you meant “when the user submits the form”, then /u/saintpetejackboy‘s answer is good.
I went through the same thought process about this post, but I re-read it and came to the conclusion similar to yours: there is no way the radio button can be active if he does not yet know how to pass data between pages – so checking it on load would only be referencing his hard-coded value for the radio buttons :/ lol
However, this seems like an unlikely scenario, as when this code is run, the user won’t have had a chance to change anything, so you know it’s not checked (presuming it started not checked).
sometimes on a page reload (without submitting), radios may still be checked. may also run into autocomplete issues. so its rare, but there may be an edge case.
You are going to want the submit button to trigger a check of the value that is stored there. On your submit button, prevent the default behavior and then grab the value from the radio button and process it from there.
My personal recommendation, would be to have your submit button prevent default, check the value of the radio button(s), and then send them via xmlhttprequest or whatever to a secondary page that process the information you are sending via POST. In this way, you can populate a DIV on the current page with your desired feedback to the user – allowing them to make whatever changes are necessary.
If you are using jquery, it is even easier because the code is a bit less.
Checking for the state to change is useful, but not if you aren’t doing anything with the changed state – you can also have the state change send the data to another page. You can do all of this entirely in javascript, but for backend processing stuff, you might want to look at a language like PHP or something similar.
The workflow is like this: your page the user sees (the “view”) has mechanics behind it (post.php, or whatever) that it uses to bounce the variables to, using POST or GET (if they are not sensitive, but I’d avoid using GET, personally, it isn’t necessary and is generally a bad practice, IMO).
Your view page can send those variables ever using jQuery – you can send all the variables from the entire form as an array, and your other page can simply (For testing purposes) just echo out all the POST variables (in PHP this would be like <?php print_r($_POST); ?>
When the state changes or the form is submitted, jquery sends these variables over, but then also loads the content of the page into a specific <div> you have, like output.
This way, you can do a check and say something like if ($_POST[‘key’] == ‘value’) { //echo something to the user; }, allowing you to properly parse the data the user is giving you, potentially even in real-time.
Thank you for the detailed reply! I’ll see what I can come up with.
Members
Online