1

i have developed a web application using mvc4.

in this case i need to get a text box value(actually the text entered in the text box) using javascript.

here is the code i am using

    @model PortalModels.WholeSaleModelUser
    @using (Html.BeginForm("uploadFile", "WholeSaleTrade", FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
        @Html.ValidationSummary(true)

    <fieldset>
        <legend>WholeSaleModelUser</legend>
        <table>
            <tr>
                <td>
                    <div class="editor-label">
                        @Html.LabelFor(model => model.Name)
                    </div>
                </td>
                <td>
                    <div class="editor-field">
                        @Html.TextBoxFor(model => model.Name)
                        @Html.ValidationMessageFor(model => model.Name)
                    </div>
                </td>
            </tr>
</table>
        <div id="partial">
            <table>
                <tr>
                    <td>
                        <img id="blah" src="../../Images/no_image.jpg" alt="your image" height="200px" width="170px" />
                    </td>
                </tr>
</table>

    <script type="text/javascript">
        function loadUserImage() {
            var userImg = document.getElementById("blah");
            var imgNm = $("#Name").value;
            userImg.src = "D:/FEISPortal/FortalApplication/Img/" + imgNm + ".png";
            alert(imgNm);
            alert(userImg.src);
        }
    </script>

in that case alert gives the value as "undefined" and if do the following modification alert gives nothing.

var imgNm = document.getElementById("Name").value; 

for

var imgNm = $("#Name").value;

how to get the text entered in the text box?

4
  • change $("#Name").value; to $("#Name").val(); Commented Jun 27, 2013 at 6:46
  • First, give specific id like, @Html.TextBox("Name", Model.Name, new { id = "UserName" }). Second get value using jquery like, $('#UserName').val()... Commented Jun 27, 2013 at 6:50
  • Possible duplicate stackoverflow.com/questions/463506/… Commented Jun 27, 2013 at 7:16
  • i can already have the value but how to handle the onchange event for any elements in PARTIAL VIEW.. that is the main problem i have.. Commented Jun 27, 2013 at 8:56

3 Answers 3

5

You should use val() function for that:

var imgNm = $("#Name").val();
Sign up to request clarification or add additional context in comments.

4 Comments

but the case how to get the value after page loads or textbox textchange even fires?? really need a answer..
yeah i think so.. but sir i'm using this script in my partial view page so how can i handle the change event using id or name of the name text box of my partial view? please help
i can already have the value but how to handle the onchange event for any elements in PARTIAL VIEW.. that is the main problem i have..
@sanzy You can select all inputs inside the form and bind an event handler.
2

If you're using native javascript you should use this:

var userImgNameElement = document.getElementById("userImgNameId");
var userImgNameValue = userImgNameElement.getAttribute("value");

With "onChanged" event:

addEvent(document.getElementById('userImgNameId'), 'change', function(event) {
    var userImgNameValue = event.target.getAttribute("value");
});

2 Comments

yeah i think so.. but sir i'm using this script in my partial view page so how can i handle the change event using id or name of the name text box of my partial view? please help and refer the code my partial view
i can already have the value but how to handle the onchange event for any elements in PARTIAL VIEW.. that is the main problem i have..
1

In an input element, the value of can be found in a value property of that element. It can therefore be retrieved via document.getElementById('idOfElement').value.

In an textarea element has it's value between the starting and the closing tag. Therefore, the value property is empty. In raw javascript, you would be able to retrieve the contents with document.getElementById('idOfElement').innerText.

If you are however using jQuery, Ufuk's solution with .val() is much easier to use.

3 Comments

yeah i think so.. but sir i'm using this script in my partial view page so how can i handle the change event using id or name of the name text box of my partial view? please help and refer the code my partial view
i can already have the value but how to handle the onchange event for any elements in PARTIAL VIEW.. that is the main problem i have..
I have no idea what you mean with 'partial view'. You can find more information about the method .on() you need to use in the documentation. As far as I am aware you didn't mention anything about event handlers or 'onchange' handlers in your question, so I would suggest making a new question where you explain very clearly what the exact problem is if you've read the documentation and googled some more, and still can't figure out what you need to do.

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.