How can you apply a jQuery event handler to act only on the div the user is in? -


here's i'm trying do, have several multiple choice questions set using same html code, same class name (a div question 4 paragraphs within div answer choices).

i want use jquery assign class called 'selected' answer choice (paragraph) user clicks on. , once they've clicked on answer choice, turn off click event question only. i'm not sure how apply div user clicking in , not entire page. here's have:

$(document).ready(function() {     function selecting() {         $('.choices p').on('click', function() {             $('.choices p').removeclass('selected');             $(this).addclass('selected');         });          $('.submit').click(function() {             $('.choices p').off('click');         });     };      selecting(); }); 

you can use .closest() find current .choices element , target p element element like

$(document).ready(function() {      function selecting() {      $('.choices p').on('click', function() {        $(this).closest('.choices').find('p').removeclass('selected');        $(this).addclass('selected');        });        $('.submit').click(function() {        $(this).prev('.choices').find('p').off('click');      });    };      selecting();    });
* {    box-sizing: border-box;    -webkit-box-sizing: border-box;    -moz-box-sizing: border-box;  }  html,  body {    height: 100%;    margin: 0;    padding: 0;  }  div {    width: 100%;    min-height: 200px;    border: 1px dashed black;  }  p {    border: 1px dashed green;    width: 49.8%;    min-height: 100px;    margin: 0;    padding: 0;    display: inline-block;    text-align: center;  }  {    text-decoration: none;    color: black;  }  .center {    width: 70%;    height: 100%;    margin: 2% auto;  }  .question {    border: 1px dashed red;  }  .selected {    background: yellow;  }  .move {    width: 15%;    min-height: 40px;    margin: 0 auto;    border: 2px solid blue;    text-align: center;    line-height: 100%;  }  input[type='submit'] {    width: 15%;    min-height: 40px;    margin-left: 42.5%;    margin-top: 2%;    border: 2px solid blue;    text-align: center;    line-height: 100%;    font-size: 2em;  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>    <div class='section' id='s1'>    <div class='center' id='center1'>      <div class='question' id='q1'>      </div>      <div class='choices' id='choices1'>        <p class='p-choice' class='a'>choice a</p>        <p class='p-choice' class='b'>choice b</p>        <p class='p-choice' class='c'>choice c</p>        <p class='p-choice' class='d'>choice d</p>      </div>      <!--<div class='move'>submit</div>-->      <input type='submit' value='submit' class='submit' />    </div>  </div>    <div class='section' id='s2'>    <div class='center' id='center2'>      <div class='question' id='q2'>      </div>      <div class='choices' id='choices2'>        <p class='p-choice' class='a'>choice a</p>        <p class='p-choice' class='b'>choice b</p>        <p class='p-choice' class='c'>choice c</p>        <p class='p-choice' class='d'>choice d</p>      </div>      <!--<div class='move'>submit</div>-->      <input type='submit' value='submit' class='submit' />    </div>  </div>


Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -