Jump to content
Sign in to follow this  
irwingb

Href to checked input radio

Recommended Posts

I have an Input form type radio, and I have a link to this form in another page; What i'd ilke to do, is that when people press this outside link and go to the form, one of the options be checked.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...