-6

I have three different named checkboxes like this

    <label><input type="radio" name="selling" value="1" />Parduodu</label><br>
    <label><input type="radio" name="trade" value="1" />Keičiu</label><br>
    <label><input type="radio" name="gift" value="1" />Dovanoju</label>

How can I make that person could choose only one out of three ? It's important that the names of the radio's should be different

frenchbaguette
  • 1,297
  • 6
  • 22
  • 41
  • use a change handler and then mark others are unchecked – Arun P Johny Aug 12 '15 at 08:36
  • 2
    why can't use use the same name – Arun P Johny Aug 12 '15 at 08:38
  • 2
    Downvoted because this violates the HTML spec. – JBert Aug 13 '15 at 07:44
  • 2
    I wasn't clear enough in my last comment: having three different names for for the radio buttons means you have 3 decoupled lists like the HTML spec prescribes. Making only 1 item selectable goes against HTML's nature, especially when you could use the same name for all and use different values to get the desired result. – JBert Aug 13 '15 at 09:10
  • 1
    HTML 2.0 introduced the format in 1995, and it has remained the same since. – Jason Spicer Aug 13 '15 at 11:55
  • 2
    possible duplicate of [make checkbox behave like radio buttons with javascript](http://stackoverflow.com/questions/5839884/make-checkbox-behave-like-radio-buttons-with-javascript) – Powerlord Aug 13 '15 at 13:50

2 Answers2

8

You should be declaring it as follows:

<label><input type="radio" name="type" value="selling" />Parduodu</label><br>
<label><input type="radio" name="type" value="trade" />Keičiu</label><br>
<label><input type="radio" name="type" value="gift" />Dovanoju</label>

Then examine $_POST['type'] for the value.

Jason Spicer
  • 249
  • 1
  • 8
  • When user has clearly explained in the question that " It's important that the names of the radio's should be different" why you kept all names same. It is not making any sense at all. – Vivek Gupta Aug 13 '15 at 11:10
  • The user explained it, however the reality is, this is how HTML specified this should be done when it was introduced in HTML 2.0 in 1995. There's even examples in the HTML 2.0 spec explaining how to use radio buttons. Ditching specification and having to work around it in JavaScript is not how you're supposed to do this, which is why the question has been downmarked repeatedly because it's a bad question. Sometimes the correct answer is to point out why it's a bad question. – Jason Spicer Aug 13 '15 at 11:55
  • This will answer you http://stackoverflow.com/questions/5839884/make-checkbox-behave-like-radio-buttons-with-javascript . – Vivek Gupta Aug 13 '15 at 12:18
  • 2
    Making a set of checkboxes behave like radio buttons is not the same as making _radio buttons_ behave like radio buttons. And HTML already has facilities for making radio buttons work like radio buttons... follow the spec for it. – Jason Spicer Aug 13 '15 at 13:24
  • @VivekGupta You can have different IDs for radio buttons even if they have the same name. – Powerlord Aug 13 '15 at 13:48
  • Dear @Powerlord clearly user said " It's important that the names of the radio's should be different". what I can do about that, may be he is getting the HTML from some third party which he can't control. – Vivek Gupta Aug 14 '15 at 05:26
  • If he can't control the HTML to the point of not being able to change the names to use the specification correctly, how would he be able to change it to put classes in? If he can put classes in, he can put in the correct names in the first place. – Jason Spicer Aug 14 '15 at 08:36
  • @JasonSpicer have you used any tool like OBIEE or SAP reporting where UI is not controlled by user but he may need to do fire some events as per the requirement. If not then I guess there is no point in discussing with you merely because you have not red the question user asked and user have not accepted your answer. Have fun – Vivek Gupta Aug 14 '15 at 10:08
  • If the UI is not controller by the user, how could the user implement your answer? Your answer required changing the markup too. – Jason Spicer Aug 14 '15 at 10:20
  • Go search for OBIEE reporting. where user got some text boxes to add tweaks to the UI but not enter UI is controlled by him. And if the user has explicitly written that " It's important that the names of the radio's should be different" it means he knows that radio button should have same name but there must be some reason why he needs such functionality. He might not be a frog in the pond :) – Vivek Gupta Aug 14 '15 at 11:11
  • Interesting that you mention OBIEE and SAP - if it were tagged with those, I might agree with you, but this is tagged PHP - which makes me think it's a PHP application that is being dealt with where changing the markup is almost certainly possible and it's based on a misunderstanding of how radio buttons are supposed to work. – Jason Spicer Aug 14 '15 at 11:17
2

Based on this answer by D.A.V.O.O.D, you can use jQuery for this. Just add a class (for example "abc") to your checkboxes like:

<label><input type="radio" name="selling" value="1" class="abc" />Parduodu</label><br>
    <label><input type="radio" name="trade" value="1" class="abc" />Keičiu</label><br>
    <label><input type="radio" name="gift" value="1" class="abc" />Dovanoju</label>

and use the following jQuery:

$(".abc").each(function()
{
    $(this).change(function()
    {
        $(".abc").prop('checked',false);
        $(this).prop('checked',true);
    });
});
Community
  • 1
  • 1
Vivek Gupta
  • 955
  • 4
  • 7
  • Thanks! This worked! I don't know why I don't thought about classes.. I will accept answer in 9 minutes – frenchbaguette Aug 12 '15 at 08:41
  • Of course, it doesn't work when JavaScript is disabled, and requires loading jQuery to replace standard browser handling. – Jason Spicer Aug 12 '15 at 09:09
  • 2
    Downvoted because this is setting a bad example, the correct way would be to follow the HTML spec by setting all the names the same. – JBert Aug 13 '15 at 07:44
  • @JBert you know there is no correct or wrong way in software development. There is only recommended ways. But in the end you need to get your work done. – Vivek Gupta Aug 13 '15 at 07:46
  • @VivekGupta There can be a wrong way if it means you take more effort than doing it the recommended way. Then again, I don't know the asker's context so I guess we might have to agree to disagree. – JBert Aug 13 '15 at 08:30
  • @JBert It can be more efforts in your terms but for me and for the asker it saved a lot of time. But you are right we might have to agree to disagree but downvote is not the right choice as we all are here to help others ISN'T? – Vivek Gupta Aug 13 '15 at 08:32
  • @VivekGupta I'm afraid I disagree again, I'm here to help people in the future who dig up existing questions by being honest about recommended solutions when they would find this question, not help people find a quick workaround when what they are doing is already not recommend. According to the etiquette FAQ, voting and commenting is one way to do that: http://stackoverflow.com/help/behavior – JBert Aug 13 '15 at 09:07
  • @JBert You need to check "Be Honest" section for yourself, I am afraid you are not following. "Above all, be honest. If you see misinformation, vote it down. Add comments indicating what, specifically, is wrong." neither my answer is wrong nor it is a misinformation. There are n number of ways of doing anything. and If out that one way which is not known to you can't be wrong – Vivek Gupta Aug 13 '15 at 09:12
  • It's wrong because it's requiring JavaScript and jQuery to do something outlined in the HTML specification - and more important to do something against how the specification says it should be done, for no technically sound reason. Yes, technically it does answer the question as asked - but not all questions are good and sometimes the correct answer is to rephrase or rethink the question. – Jason Spicer Aug 13 '15 at 10:09
  • My dear friends there might be some scenario where html control is not in your hand where in you can edit the html like if you need to edit GUI of OBIEE. I am not able to understand asker is happy with the answer, why you guys are making an issue out of it. – Vivek Gupta Aug 13 '15 at 10:16
  • Because if a tool is generating this HTML, it is doing it wrong, simple as that. There are many routes to a solution in computer science, but that does not mean all solutions are legitimate. – Jason Spicer Aug 13 '15 at 11:04
  • When user has clearly explained in the question that " It's important that the names of the radio's should be different" you kept all names same. It is not making any sense at all. and you think your answer is LEGITIMATE. – Vivek Gupta Aug 13 '15 at 11:10
  • My answer explains how it was always supposed to be done. Doing anything else means inventing ways to bypass how HTML was designed. – Jason Spicer Aug 13 '15 at 13:26