I would like to use these DIV's as options. For this reason, the DIV
which is being clicked should be selectable (leaving others non
selected - only one should be active)
You shouldn't be using divs as options. This is not semantic. Radios (and checkboxes) are elements which are there for that purpose. divs are meant for dividing your content into sections and that is where you should stop. You use radios and checkboxes for form elements. There are many other problems of abusing elements for things which they are not meant for:
- You end up with lot of extraneous code just to force
divs into doing what they are not supposed to do. Maintaining state of selection, customizing presentation, etc. This code over a period of time becomes convoluted and difficult to maintain.
- You also need extra code to then post the values back to the server-side code along with form data. You will go into hoops to coerce
div states into meaningful values, something which form elements provide you with out-of-the-box. UI no longer remains clean.
- Imagine some other developer tasked to maintain your code. (S)he will waste a lot of productive time in just trying to decipher where are those magic values coming from. And then making even small changes will be a burden. When you use form elements, all user-collected values are in same place and visible clearly as form elements in code. It then becomes very easy to decipher where all those values are coming from.
Your comment:
I would like to use DIV for selection.. Just because I customized it
on good level and I believe this selection can be possible for DIV
elements as well - apart from other options like radio buttons - check
boxes etc.
Just because you believe that something can be possible, doesn't mean you should be doing it. Yes, it is possible. But, it is not the intended purpose of HTML.
Solution:
I assume that the only reason you want to use divs for options, is that you are able to customize it. Well, you can customize radios and checkboxes too. Using form elements will save you from a lot of code, and also you do not need Javascript at all because the values will be part of the formData and will be available server-side with zero effort.
Just use proper radio (or checkbox if multiple options are needed), add related labels, and then stylize them. That's all to it.
Using Radios:
Javascript used only to display values client-side for demo
Example Fiddle 1: http://jsfiddle.net/abhitalks/xg3dwme7/1/
Snippet 1:
function getOptions() {
var elems = document.getElementById('myform').elements;
var sel = elems['myOptions'].value;
alert(sel);
}
* { box-sizing: border-box; margin: 0; padding: 0; }
ul.mySection { margin: 16px; list-style: none; }
ul.mySection li { margin: 0px 0px; display: inline-block; }
ul.mySection input[type=radio] { display: none; }
ul.mySection label {
display: table-cell; cursor: pointer;
width: 200px; height: 50px;
vertical-align: middle; text-align: center;
background-color: rgba(0, 0, 255, 0.4);
}
ul.mySection label:hover {
background-color: rgba(0, 0, 255, 1); color: #fff; transition: all 0.25s;
}
ul.mySection input[type=radio]:checked ~ label {
background-color: rgba(50, 200, 50, 1);
}
input#btn { margin: 8px 18px; padding: 8px; }
<form id="myform">
<ul class="mySection">
<li>
<input type="radio" id="r1" name="myOptions" value="Computers" checked />
<label for="r1">Computers</label>
</li>
<li>
<input type="radio" id="r2" name="myOptions" value="Electronics" />
<label for="r2">Electronics</label>
</li>
<li>
<input type="radio" id="r3" name="myOptions" value="Mechanical" />
<label for="r3">Mechanical</label>
</li>
<li>
<input type="radio" id="r4" name="myOptions" value="Electrical" />
<label for="r4">Electrical</label>
</li>
</ul>
<input id="btn" type="button" value="confirm" onclick="getOptions();" />
</form>
And if you at all need to change it to multiple selections, you can do so very easily by just changing radio to checkbox without any change to the code and it will all work.
Using Checkboxes:
Javascript used only to display values client-side for demo
*Example Fiddle 2: http://jsfiddle.net/abhitalks/th3yqkq0/4/ *
Snippet 2:
function getOptions() {
var frm = document.getElementById("myform");
var chk = frm.querySelectorAll('input[type=checkbox]:checked');
var vals = [];
for (var i=0; i<chk.length; i++) {
vals.push(chk[i].value);
}
alert(JSON.stringify(vals));
}
* { box-sizing: border-box; margin: 0; padding: 0; }
ul.mySection { margin: 16px; list-style: none; }
ul.mySection li { margin: 0px 0px; display: inline-block; }
ul.mySection input[type=checkbox] { display: none; }
ul.mySection label {
display: table-cell; cursor: pointer;
width: 200px; height: 50px;
vertical-align: middle; text-align: center;
background-color: rgba(0, 0, 255, 0.4);
}
ul.mySection label:hover {
background-color: rgba(0, 0, 255, 1); color: #fff; transition: all 0.25s;
}
ul.mySection input[type=checkbox]:checked ~ label {
background-color: rgba(50, 200, 50, 1);
}
input#btn { margin: 8px 18px; padding: 8px; }
<form id="myform">
<ul class="mySection">
<li>
<input type="checkbox" id="r1" name="myOptions" value="Computers" />
<label for="r1">Computers</label>
</li>
<li>
<input type="checkbox" id="r2" name="myOptions" value="Electronics" />
<label for="r2">Electronics</label>
</li>
<li>
<input type="checkbox" id="r3" name="myOptions" value="Mechanical" />
<label for="r3">Mechanical</label>
</li>
<li>
<input type="checkbox" id="r4" name="myOptions" value="Electrical" />
<label for="r4">Electrical</label>
</li>
</ul>
<input id="btn" type="button" value="confirm" onclick="getOptions();" />
</form>
Hope that helps.
.
divfor options? Why not simple radio buttons which are meant for that purpose?