Link to home
Start Free TrialLog in
Avatar of ICT Department
ICT Department

asked on

How to toggle the placeholder of a textarea element in jquery?

Hi,

I have a good working function:

$("#TogglePat").click(function(){
  $("#PatIcon").toggleClass("fa-toggle-off fa-toggle-on",1000);
  $(".PatField").toggle("slow");
  $("#ssBezorgadres option[value='Patiënt']").prop('disabled',function(i,v){return!v;});
};

I have a textarea element with a placeholder in place, on click of the above function I want the content of the placeholder to change (need to remove a bit if tekst) and be placed back when I click it again.
Hoe do I do this?
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Couple of ways to do it, but they'll all be based around setting the placeholder property on your TextArea:

$('#myTextArea').prop('placeholder', 'Some PlaceHolder Text');

To toggle it you'd need to know the 2 values, and how you do this would depend on your requirements. You'd also need to decide on what constitutes a toggle (on/off state). You're toggling a class on PatIcon, so you could potentially check if that element has a class and go from there. Here a very simple example

$("#PatIcon").toggleClass("fa-toggle-off fa-toggle-on",1000);
var placeholderText = $("#PatIcon").hasClass('fa-toggle-on') ? "Text if it has the class" : "Text if it doesn't have the class";
$('#myTextArea').prop('placeholder', placeholderText);

Open in new window

Avatar of ICT Department
ICT Department

ASKER

I will check this...
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.