0

.unselectable {
  -webkit-user-select: none;  /* Chrome all / Safari all */
  -moz-user-select: none;     /* Firefox all */
  -ms-user-select: none;      /* IE 10+ */
  user-select: none;          /* Likely future */       
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script>
        $(function () {
            var $chk = $("#grpChkBox input:checkbox");
            var $tbl = $("#someTable");

            $chk.prop('checked', true);

            $chk.click(function () {
                var colToHide = $tbl.find("." + $(this).attr("name"));
                $(colToHide).toggle();
            });
        });
    </script>
 <div id="grpChkBox">
	<table border="0">
  <tbody>
    <tr>
      <td><input type="checkbox" name="Column_1" /> Column 1</td>
    </tr>
    <tr>
      <td><input type="checkbox" name="Column_2" /> Column 2</td>
    </tr>
    <tr>
      <td><input type="checkbox" name="Column_3" /> Column 3</td>
    </tr>
    <tr>
      <td><input type="checkbox" name="Column_4" /> Column 4</td>

    </tr>
  </tbody>
</table>
</div>

<table border="1" cellpadding="3" cellspacing="2" id="someTable">
  <tr bgcolor="#CCCCCC" class="th">
    <th class="Column_1">Column 1</th>
    <th class="Column_2">Column 2</th>
    <th class="Column_3">Column 3</th>
    <th class="Column_4">Column 4</th> 
  </tr>
    <tr>
      <td class="Column_1">Data 1</td>
      <td class="Column_2">Data 2</td>
      <td class="Column_3">Data 3</td>
      <td class="Column_4">Data 4</td>
    </tr>
</table>

This script Shows or Hides a column in a table. When the checkbox is not checked and the column is hidden, I would like to add class="unselectable" to the column.

Example: If Column 1 was hidden its class would be class="Column_1 unselectable"

2
  • You want add class to the column and pretend it from hiding? Commented Apr 2, 2018 at 14:36
  • 1
    Why do you want to add user-select: none; do something that's hidden (display: none;)? Commented Apr 2, 2018 at 15:10

1 Answer 1

2

Modify this line:

$(colToHide).toggle();

...to be this:

$(colToHide).toggle().toggleClass('unselectable');

See below for live implementation.

Alternatively, you could remove the .toggle() from your JS and leave only the toggleClass('unselectable') function, and simply add display: none to the .unselectable class in your CSS.

.unselectable {
  -webkit-user-select: none;
  /* Chrome all / Safari all */
  -moz-user-select: none;
  /* Firefox all */
  -ms-user-select: none;
  /* IE 10+ */
  user-select: none;
  /* Likely future */
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
  $(function() {
    var $chk = $("#grpChkBox input:checkbox");
    var $tbl = $("#someTable");

    $chk.prop('checked', true);

    $chk.click(function() {
      var colToHide = $tbl.find("." + $(this).attr("name"));
      $(colToHide).toggle().toggleClass('unselectable');
    });
  });
</script>
<div id="grpChkBox">
  <table border="0">
    <tbody>
      <tr>
        <td><input type="checkbox" name="Column_1" /> Column 1</td>
      </tr>
      <tr>
        <td><input type="checkbox" name="Column_2" /> Column 2</td>
      </tr>
      <tr>
        <td><input type="checkbox" name="Column_3" /> Column 3</td>
      </tr>
      <tr>
        <td><input type="checkbox" name="Column_4" /> Column 4</td>

      </tr>
    </tbody>
  </table>
</div>

<table border="1" cellpadding="3" cellspacing="2" id="someTable">
  <tr bgcolor="#CCCCCC" class="th">
    <th class="Column_1">Column 1</th>
    <th class="Column_2">Column 2</th>
    <th class="Column_3">Column 3</th>
    <th class="Column_4">Column 4</th>
  </tr>
  <tr>
    <td class="Column_1">Data 1</td>
    <td class="Column_2">Data 2</td>
    <td class="Column_3">Data 3</td>
    <td class="Column_4">Data 4</td>
  </tr>
</table>

Sign up to request clarification or add additional context in comments.

1 Comment

That is exactly what I needed! Thank you so much. I want the text in those hidden columns to not be selected during a cut and paste. This works perfect for my needs...

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.