0

I have below html layout. When i enters a, b or c in to the text box (#txtprevent). i need to stop the user from selecting the specific item which has the data-attribute value. e.g.: if i enter b. i should not be allowed to select b. The problem have is event.preventDefault(); is not stopping the selection action. If this the expected functionality.

If there a different workaround for this?

Below is my Html

<input id="txtprevent" type="text">
<table id="mySelectable" class="ui-selectable">
    <tbody><tr id="1" data-myapp="a" class="ui-selectee">
        <td>Row 2</td>
    </tr>
    <tr id="2" data-myapp="b" class="ui-selectee">
        <td>Row 3</td>
    </tr>
    <tr id="3" data-myapp="c" class="ui-selectee">
        <td>Row 1</td>
    </tr>
</tbody></table>

My js

    $(function () {
      $('#mySelectable').selectable({
          filter: "tr",
          selecting: function (event, ui) {
              var item = $(ui.selecting);
              var itemVal = item.prop('data-myApp');
              if(itemVal == $('#txtprevent').val()){
                  //does not stop the selection process
                  event.preventDefault();
              }
           }
       });
    });

2 Answers 2

1

simply return false

if(itemVal == $('#txtprevent').val()){
   //does not stop the selection process
   return false;
}
Sign up to request clarification or add additional context in comments.

Comments

0

I found a workaround for this, instead of using the 'selecting' event i use the 'selected' event and remove the 'ui-selected' class of the selected item. Below is the implementation

$(function () {
      $('#mySelectable').selectable({
          filter: "tr",
          selected: function (event, ui) {
              var item = $(ui.selected);
              var itemVal = item.prop('data-myApp');
              if(itemVal == $('#txtprevent').val()){
                    //Can give any msg if required.
                    //Remove the selected class
                    item.removeClass('ui-selected');
              }
           }
       });
    });

Comments

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.