1

Iam trying to do multiple select with JavaScript. but can't working.how can i fix this? Iam done it in following way

 var expanded = false;
        function showCheckboxes() {
            var checkboxes = document.getElementById("checkboxes");
            if (!expanded) {
                checkboxes.style.display = "block";
                expanded = true;
            } else {
                checkboxes.style.display = "none";
                expanded = false;
            }
        }
.multiselect {
            width: 200px;
        }
        .selectBox {
            position: relative;
        }
        .selectBox select {
            width: 100%;
            font-weight: bold;
        }
        .overSelect {
            position: absolute;
            left: 0; right: 0; top: 0; bottom: 0;
        }
        #checkboxes {
            display: none;
            border: 1px #dadada solid;
        }
        #checkboxes label {
            display: block;
        }
        #checkboxes label:hover {
            background-color: #1e90ff;
        }
<div class="multiselect">
                <div class="selectBox" onclick="showCheckboxes()">
                    <select>
                        <option>check box1</option>
                    </select>
                    <div class="overSelect"></div>
                </div>
                <div id="checkboxes">
                    <label for="one"><input type="checkbox" id="one" />First checkbox</label>
                </div>
            </div>
<div class="multiselect">
                <div class="selectBox" onclick="showCheckboxes()">
                    <select>
                        <option>check box2</option>
                    </select>
                    <div class="overSelect"></div>
                </div>
                <div id="checkboxes">
                    <label for="one"><input type="checkbox" id="one" />First checkbox</label>
                </div>
            </div>
<div class="multiselect">
                <div class="selectBox" onclick="showCheckboxes()">
                    <select>
                        <option>check box3</option>
                    </select>
                    <div class="overSelect"></div>
                </div>
                <div id="checkboxes">
                    <label for="one"><input type="checkbox" id="one" />First checkbox</label>
                </div>
            </div>

6
  • Identifiers in HTML must be unique. It will make the HTML document invalid Commented Apr 12, 2016 at 6:18
  • 3 divs with same id .it's invalid .when you call get id ,1st one is taken Commented Apr 12, 2016 at 6:21
  • i need dynamic multiple select.how ? Commented Apr 12, 2016 at 6:23
  • there is lot of ways to do it.you can get clicked element .or you can pass parameter to showCheckboxes(2) . Commented Apr 12, 2016 at 6:25
  • ID must be unique You should avoid inline-event-binding. jQuery can be handy to deal with complex DOM manipulations.. Commented Apr 12, 2016 at 6:26

2 Answers 2

1

you cannot have same id for multiple elements .it's invalid . however when you call getElementById() 1st element is taken .that's why in your example only 1st element expand and collapse .

here is example . in this example i pass a parameter to showCheckboxes methods.so we can select correct checkboxes .also we have to store state of 3 elements individually.

https://jsfiddle.net/93894gbs/5/

js

var expanded = [false, false, false];
var checkboxes;

function showCheckboxes(i) {
  checkboxes = checkboxes || document.getElementsByClassName("checkboxes");
  if (!expanded[i]) {
    checkboxes[i].style.display = "block";
    expanded[i] = true;
  } else {
    checkboxes[i].style.display = "none";
    expanded[i] = false;
  }
}
Sign up to request clarification or add additional context in comments.

8 Comments

how can get input values of multiple input case?
@noushidp i don't get it ?can you clarify
i need to get the values of check boxes.
@noushidp you can get it using index i .see jsfiddle.net/93894gbs/4 this will alert the value of check box inside checkboxes div
how can get value of multiple input box in one check box ?
|
0

Also you can pass "event" attribute while calling the handler like :

div class="selectBox" onclick="showCheckboxes(event)"

and change javascript like :

var expanded = false;
function showCheckboxes(ev) {
    console.log("ev", ev);

    var checkboxes = ev.currentTarget.parentElement.lastElementChild;
    if (!expanded) {
        checkboxes.style.display = "block";
        expanded = true;
    } else {
        checkboxes.style.display = "none";
        expanded = false;
    }
}

1 Comment

When you send the event object as paramenet in "showCheckboxes(event)" function, you can access the element on which the click happened using : ev.currentTarget and then taking its parent node's last child using: ev.currentTarget.parentElement.lastElementChild which will be the div with id="checkboxes", you can execute rest of the function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.