0

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"

7
  • 1
    In what way is your code not working as expected? Can you provide a runnable minimal reproducible example which demonstrates the problem and indicate what specifically isn't working as expected in that example? As an aside... runat="server" is not a client-side HTML attribute and will do absolutely nothing here. Given the asp.net tag, you may be confusing the difference between client-side code and server-side code. Commented Mar 21, 2023 at 14:48
  • Your last sentence doesn't make much sense, what's the issue with the code? Commented Mar 21, 2023 at 15:04
  • As @David says, if you want to use those input fields in code behind on PostBack, you need to add 'real' aspnet webform controls dynamically. Not as string values. Or put them in a GridView, Repeater etc. Commented Mar 21, 2023 at 15:11
  • For what it's worth, your code demonstrably works as-is, so it's difficult for us to guess what problem you're trying to describe. Commented Mar 21, 2023 at 15:14
  • thnks @David the script actually works i just have to remove runat="server" Commented Mar 21, 2023 at 15:26

2 Answers 2

1

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>
Sign up to request clarification or add additional context in comments.

1 Comment

Ok now I update my answer using concatenation
0

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+'" />'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.