Doug Hemingway
Doug Hemingway

Reputation: 55

How do I get the parent id in this function?

The attached code properly returns the id and the value of the checked box. I need to get the id of the enclosing div so that I can set the display attribute to hidden.

<!DOCTYPE html>

<html>
<head>
</head>
<body>
    <div id="wrapper">
        <form>
            <div id="boatdiv1"><input type="checkbox" name="cb" id="boat1" value="123" onclick='doClick();'><label for='boat1'>boat1</label><br></div>
            <div id="boatdiv2"><input type="checkbox" name="cb" id="boat2" value="456" onclick='doClick();' onclick='doClick();'><label for='boat2'>boat2</label><br></div>
            <div id="boatdiv3"><input type="checkbox" name="cb" id="boat3" value="789" onclick='doClick();'><label for='boat3'>boat3</label><br></div>
        </form>
    </div>


</body>
<script>
    function doClick() {
        var checkedValue = null; 
        var inputElements = document.getElementsByName('cb');
        for(var i=0; inputElements[i]; ++i){
              if(inputElements[i].checked){
                   checkedValue = inputElements[i].value;
                   checkedID = inputElements[i].id;
                   console.log('checked id = '+checkedID);
                   console.log('value = '+checkedValue);
                   break;
              }
        }
              ParentID = checkedID.offsetParent;
              console.log(ParentID.id);
    }
</script>
</html>

I expected that ParentID would return the id. Instead, I get an error "TypeError: undefined is not an object (evaluating 'ParentID.id')"

Upvotes: 0

Views: 348

Answers (2)

zer00ne
zer00ne

Reputation: 43910

You need to remove the onevent attributes and use either an onevent property or event listener instead:

<input doClick()...>

This is basically what you need to hide the parent element of clicked element (event.target):

event.target.parentElement.style.display = 'none';

Demo

Details commented in demo

// Reference the form 
var form = document.forms[0];

// Register the form to the change event
form.onchange = hide;

/*
Called when a user unchecks/checks a checkbox
event.target is always the currently clicked/changed tag
Get the changed parent and set it at display: none
*/
function hide(e) {
  var changed = e.target;
  changed.parentElement.style.display = 'none';
  console.log(`Checkbox: ${changed.id}: ${changed.value}`);
  console.log(`Parent: ${changed.parentElement.id}`);
}
<!DOCTYPE html>
<html>

<head></head>

<body>
  <div id="wrapper">
    <form>
      <div id="boatdiv1"><input type="checkbox" name="cb" id="boat1" value="123"><label for='boat1'>boat1</label><br></div>
      <div id="boatdiv2"><input type="checkbox" name="cb" id="boat2" value="456"><label for='boat2'>boat2</label><br></div>
      <div id="boatdiv3"><input type="checkbox" name="cb" id="boat3" value="789"><label for='boat3'>boat3</label><br></div>
    </form>
  </div>
</body>

</html>

Upvotes: 1

Jack Bashford
Jack Bashford

Reputation: 44105

Use parentNode - also note that checkedID is a string, so it doesn't have a parent. Use getElementById to get the checked input:

ParentID = document.getElementById(checkedID).parentNode;
console.log(ParentID.id);

Upvotes: 0

Related Questions