javascript - Textarea placeholder with a checkbox - Stack Overflow

I'm trying to change the placeholder of a textarea with the press of a checkbox button. I just can

I'm trying to change the placeholder of a textarea with the press of a checkbox button. I just can't get it to change.

To be clear, I'm trying to set a placeholder NOT a value.

My textarea:

<textarea name="problem" id="problem" rows="3" class="form-control" cols="45" placeholder="Describe your ticket reason in as few words as possible."></textarea>

My JavaScript:

<div class="onoffswitch">
  <input type="checkbox" onchange="changeplh()" name="onoffswitch" class="onoffswitch-checkbox" value="1" id="myonoffswitch" checked>
  <label class="onoffswitch-label" for="myonoffswitch">
    <span class="onoffswitch-inner"></span>
    <span class="onoffswitch-switch"></span>
  </label>
  <script>
    function changeplh() {
      debugger;
      var sel = document.getElementById("myonoffswitch");
      var textbx = document.getElementById("problem");
      var indexe = sel.selectedIndex;

      if (indexe == 0) {
        $("#problem").val('');
        $("#problem").prop("placeholder", "age");

      }
      if (indexe == 1) {
        $("#problem").val('');
        $("#problem").prop("placeholder", "name");
      }
    }
  </script>
</div>

What I specifically need - a checkbox button that changes a placeholder of a textarea to 2 different options. EX: Click it once, it says "age", click it again it says "name", click it again it says "age" etc.

I'm trying to change the placeholder of a textarea with the press of a checkbox button. I just can't get it to change.

To be clear, I'm trying to set a placeholder NOT a value.

My textarea:

<textarea name="problem" id="problem" rows="3" class="form-control" cols="45" placeholder="Describe your ticket reason in as few words as possible."></textarea>

My JavaScript:

<div class="onoffswitch">
  <input type="checkbox" onchange="changeplh()" name="onoffswitch" class="onoffswitch-checkbox" value="1" id="myonoffswitch" checked>
  <label class="onoffswitch-label" for="myonoffswitch">
    <span class="onoffswitch-inner"></span>
    <span class="onoffswitch-switch"></span>
  </label>
  <script>
    function changeplh() {
      debugger;
      var sel = document.getElementById("myonoffswitch");
      var textbx = document.getElementById("problem");
      var indexe = sel.selectedIndex;

      if (indexe == 0) {
        $("#problem").val('');
        $("#problem").prop("placeholder", "age");

      }
      if (indexe == 1) {
        $("#problem").val('');
        $("#problem").prop("placeholder", "name");
      }
    }
  </script>
</div>

What I specifically need - a checkbox button that changes a placeholder of a textarea to 2 different options. EX: Click it once, it says "age", click it again it says "name", click it again it says "age" etc.

Share Improve this question edited Nov 11, 2020 at 14:35 Mosh Feu 29.4k18 gold badges93 silver badges141 bronze badges asked Jan 5, 2016 at 21:42 GrumpyCroutonGrumpyCrouton 8,6197 gold badges37 silver badges80 bronze badges 2
  • you have indexe = sel.selectedIndex, but the element with id of myonoffswitch is not a <select> element, also your textarea does not have an id of problem – Patrick Evans Commented Jan 5, 2016 at 21:49
  • @PatrickEvans stupid me, I posted the wrong textarea. – GrumpyCrouton Commented Jan 5, 2016 at 21:52
Add a ment  | 

1 Answer 1

Reset to default 4

Your code is a little bit messy (for example, you have no #problem in your code) so I just created a simple demo of changing the placeholder.

$('#problem').change(function() {
  $('textarea').attr('placeholder', $(this).is(':checked') ? 'name' : 'age');
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea class="form-control" style="height:150px" name="techProblem" placeholder="Describe the issue in as few words as possible."></textarea>

<label><input type="checkbox" id="problem" /> Change placeholder</label>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745139275a4613353.html

相关推荐

  • javascript - Textarea placeholder with a checkbox - Stack Overflow

    I'm trying to change the placeholder of a textarea with the press of a checkbox button. I just can

    7小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信