Get value from dynamic text field

In this code, I created a dynamic text field, now when I try to get the insert value from this text field. This gives me a null value.

 <form name="myForm">
 <textarea name="fname" <%#!((GPNS.BusinessLayer.SpecialItems.SpecialItem)Container.DataItem).Code.Equals("OTH", StringComparison.InvariantCultureIgnoreCase) ? "style='display: none;'" : string.Empty%> id="text<%#((GPNS.BusinessLayer.SpecialItems.SpecialItem)Container.DataItem).ID%>" maxlength="50" placeholder="Enter other item details"></textarea>
 </form>

This is my function to get the value from the textarea field:

 function ValidateData() {
            if ($("textarea").is(":visible")) {
                //var x = document.forms["myForm"]["fname"].value;
                var x = document.getElementsByName("fname").value;
                if (x == null || x == "") {
                    alert("Please Enter Other Item Details");
                    return false;
                }
            }
            else return true
        }
+4
source share
4 answers

Your text box was dynamic, so you can use the textarea change event. You can use this code when loading, so whenever you enter text, it sets to OtherItemValue:

var OtherItemValue;
 $("textarea").on('input change keyup', function () {
                if (this.value.length) {
                    OtherItemValue = this.value;
                } else {
                    OtherItemValue = "";
                }
            });

And then you can use below code:

function ValidateData() {
    if ($("textarea").is(":visible")) {
 if (OtherItemValue == null || OtherItemValue == "") {
            alert("Please Enter Other Item Details");
            return false;
        }
    }
    else return true
}
+2
source
<form name="myForm">
 <textarea name="fname" <%#!((GPNS.BusinessLayer.SpecialItems.SpecialItem)Container.DataItem).Code.Equals("OTH", StringComparison.InvariantCultureIgnoreCase) ? "style='display: none;'" : string.Empty%> id="text<%#((GPNS.BusinessLayer.SpecialItems.SpecialItem)Container.DataItem).ID%>" maxlength="50" placeholder="Enter other item details"></textarea>
</form>

Your function should now look like this:

function ValidateData() {
        if ($("textarea").is(":visible")) {
            //var x = document.forms["myForm"]["fname"].value;
            var x = document.getElementsByName("fname")[0].value;
            if (x == null || x == "") {
                alert("Please Enter Other Item Details");
                return false;
            }
        }
        else return true
    }

, getElementsByName , value. . , , fname .

0

jQuery , ?

:

function ValidateData() {
    if ($("textarea").is(":visible")) {
        var x = $("textarea").val();
        if (x == null || x == "") {
            alert("Please Enter Other Item Details");
            return false;
        }
    }

    return true
}

, @Rachit Gupta .

0

jquery.val() . textarea .

$(document).ready(function(){
   $("textarea[name='fname']").val("");    
});

    function ValidateData() {

    if ($("textarea").is(":visible")) {
        var x = $("textarea[name='fname']").val();
        if (x == null || x == "") {
            alert("In if " + x);
            return false;
        }
        else {
              alert("In else" + x);
        }
    }
    else {
            return true
    }
}
0

Source: https://habr.com/ru/post/1654114/


All Articles