How to add fading text in a form field (to show a suggestion)

답변함


2022년 11월 18일에 게시됨

Dear Zendesk community!

Here :

 Shows how to prepopulate a field with text.

Does some body now how to put text that fades when the user click on the field?

(like a suggestion) Here is an example (on the right side of the code) : https://developer.mozilla.org/en-US/docs/Web/CSS/::placeholder

 

Many thanks 


0

5

댓글 5개

Great ! :)

0


Thank you very much Ifra Saqlain it works perfectly!!

Best regards,

Raphaël

0


@Raphaël Péguet, try this updated code and remove previous:

    var textOpacity = document.getElementById("request_subject");
    var fieldValue = document.getElementById("request_subject").value = 'CUSTOM TEXT';
    textOpacity.style.color = "red";
  
window.addEventListener('click', function(e) {   
  if (document.getElementById('request_subject').contains(e.target)){
    textOpacity.style.opacity = "0.5";
  } else{
    textOpacity.style.opacity = "1"
  }
});

 

Team

1


Ifra Saqlain Thank you very much Ifra! 

It works but because of "onclick" function if I write over the "native" text 

Then go to another field and come back to the previous field to rewrite "the on click function" will erase what I wrote.

I did a video about it: https://www.loom.com/share/5e5d95df2e2a499787a756d44298126a

So maybe we should ask a "if" to the "onclick" to say if val = native value then fade if not don't do anything.

Best regards, 

Raphaël

 

0


Hey Raphaël Péguet,

 

Can be done something like this:

$('#request_subject').val('Product Registration').css("color","red");
 $('#request_subject').click(function(){
    $(this).val('Product Registration').css("opacity"," .5");
 });


Output:
Default



After clicking:



 

Team

0


댓글을 남기려면 로그인하세요.

원하는 정보를 못 찾으셨나요?

새 게시물