Asif Uz Zaman
Asif Uz Zaman

Reputation: 410

Placeholder in javascript with val()

I can not understand Javascript language properly, I am trying to write inside placeholder but it is not working, here is my code

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>val demo</title>

  <script src="https://code.jquery.com/jquery-1.10.2.js"></script>

  <script type="text/javascript">
        $(document).ready(function() {
            $('#myInputText').keyup(function() {
             $('#myOutputText2').val('placeholder="' + $(this).val() + '"');
             $('#myDIVTag').html('<b>' + $(this).val() + '</b>');
            });
        });     
    </script>

</head>
<body>

<input id="myInputText" type="text" name="post_title" size="79" placeholder="Enter title here" />
<p>Snippet Preview: <a href="#" id="myDIVTag"></a></p>
<input name="textfield2" type="text" id="myOutputText2" size="79" />


</body>
</html>

It is work perfectly but I want this in to placeholder mode. Please help me.

Upvotes: 0

Views: 312

Answers (1)

AmmarCSE
AmmarCSE

Reputation: 30557

To change an attribute, use attr() like

$('#myOutputText2').attr('placeholder',$(this).val());

Update

Since placeholder is a native HTML DOM property of input elements, it is better to use prop()

$('#myOutputText2').prop('placeholder',$(this).val());

For further reading, see .prop() vs .attr()

Upvotes: 2

Related Questions