skip to Main Content

I have dynamically added textbox in asp.net web form page using javascript as following

    <script type="text/javascript">
    var i = "Placehoder";
    $("#partAdder").click(function () {
        newPart =
            '<div class="row" id="row">' +
            '<div class="col-xl-2">' +
            '<div class="form-group">' +
            '<input type="text" runat="server" class="form-control form-control-sm" placeholder='+i+' />' +
            '</div>' +
            '</div>' +
            '</div>';
        $('#newistrpart').append(newPart);
     });

How to pass the i value to the script "using that script i gent +i+ instead of the value"

2

Answers


  1. I’m not sure but if I understand your JS code, you forgot to put " on your placeholder like that'<input type="text" runat="server" class="form-control form-control-sm" placeholder="'+i+'" />'

    Login or Signup to reply.
  2. you can use string interpolation

    <script type="text/javascript">
    var i = "Placehoder";
    $("#partAdder").click(function () {
        newPart = `<div class="row" id="row">
            <div class="col-xl-2">
                <div class="form-group">
                    <input type="text" runat="server" class="form-control form-control-sm" placeholder="${i}" /> // <-- update this line
                </div>
            </div>
        </div>`;
        $('#newistrpart').append(newPart);
    });
    </script>
    

    ${i} syntax is used to include the value of the i variable within the string. The backticks (`) are used to enclose the string, which allows for string interpolation.

    backtick shortcut on Windows is Alt + 96

    you can use string concatenation to achieve the same result, like this:

    <script type="text/javascript">
    var i = "Placehoder";
    $("#partAdder").click(function () {
        newPart = '<div class="row" id="row">' +
            '<div class="col-xl-2">' +
                '<div class="form-group">' +
                    '<input type="text" runat="server" class="form-control form-control-sm" placeholder="' + i + '" />' +
                '</div>' +
            '</div>' +
        '</div>';
        $('#newistrpart').append(newPart);
    });
    </script>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search