HendPro12
HendPro12

Reputation: 1104

C# Jquery Reference Error is Undefined

I have a C# MVC4 application in which I am writing a JQuery function to grab some values, post to an ActionResult and then refresh a partial view. All functionality is working except for setting a new var equal to the value of a variable within one of my div elements.

The pre-existing variable is called myName and is located in a div with an id of NameDiv.

Ive tried these four versions of code and each results in: Reference Error myName is not defined.

    var origname = myName;    
    var origname = myName.value();
    var origname = myName.val();
    var origname = $('#NameDiv').valueOf(myName);

When running the application and inspecting element, I see that myName is populating with the correct value.

Upvotes: 0

Views: 371

Answers (1)

Ian
Ian

Reputation: 50905

Use:

var origname = $('#NameDiv').find('input[name="myName"]').first().val();
// console.log(origname);

This will find the element on the page with the id of "NameDiv". Then it gets the input elements on the page with the name of "myName". Then it gets the first one found. It will then get the value of it (by using .val()), and store that value in the variable origname.

Upvotes: 2

Related Questions