Jump to content

Href to checked input radio


irwingb

Recommended Posts

The 100% fool-proof way to do that is to use some server-side scripting. If you're up for it and your server has PHP enabled, ask me about that. Otherwise, JavaScript will work. The only downside is that a very small number of people have JavaScript turned off. If that doesn't worry you, then this will work.

<input type="radio" id="male"><script type='text/javascript'>    if (location.search != "")    {        var id = location.search.split("?")[1];        document.getElementById(id).checked = "checked";    }</script>

And the link from the other page should look like this:

<a href="mypage.html?male">

The ?male is called a query string. It's normally used to send data to your server, but it can be used like this also.1. The script looks for a query string. If it doesn't find one, it terminates.2. It finds the value of the query string and strips off the ?3. It searches your document for an element whose id matches the query string. When it finds it, it sets the checked property.I chose the id male as an example. You can use anything you want. Just keep it to ordinary letters and numbers, or they will get encoded, and I didn't build that into the script. Also, to keep things simple, I put the script right after the radio element. It can go anywhere after the radio element, but not before, or it won't work. I'd have to make the script a little more complicated than it needs to be. I suggest testing it with no changes to verify that it works. Then adapt it to your needs.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...