Reputation: 1
No matter what I've tried, I cannot get the value of a textbox in my jQuery function. I know there is a value there, I can see it. However my code below comes back at "undefined" for the value. I cannot figure out why.
I have a js file that is referenced on my asp.net page. On my asp.net page, I have a button that triggers a function on click.
<asp:ImageButton ID="BtnNext" runat="server" OnClientClick="TransactionType(); return false;" ImageUrl="~/next.png" />
My jQuery code:
function TransactionType() {
var PType = $("#TxtPTypeValue").val;
alert(PType);
if (PType == "CC") {
SubmitCCTransaction();
} else if (PType == "EC") {
SubmitECheckTransaction();
};
};
The part that isn't working is this: var PType = $("#TxtPTypeValue").val;
which comes back as "undefined".
Upvotes: 0
Views: 112
Reputation: 46
$(document).ready(function(){
$('input:text').val();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" id="txtvalue" value="425656">
Upvotes: 0
Reputation: 77
You have to pass client id of you control, if you are going to use the value of a server-side control, for example :
$("#<%=TxtPTypeValue.ClientId%>").val();
Upvotes: -1
Reputation: 14198
You can fix in this way
$("#TxtPTypeValue").val();
$("#BtnNext").on("click", function(){
console.log($("#TxtPTypeValue").val());
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type = "text" id = "TxtPTypeValue" />
<button id="BtnNext">BtnNext</button>
Updated
According to @sh1rts's comment, you should also make sure that
Add a ClientIdMode="Static" to that definition, without this it won't have that ID on the actual page
Upvotes: 1
Reputation: 10569
Val
is a function, so you have to call it like this val()
.
Try the below code.
function TransactionType() {
var PType = $("#TxtPTypeValue").val();
alert(PType);
if (PType == "CC") {
// SubmitCCTransaction();
} else if (PType == "EC") {
// SubmitECheckTransaction();
};
};
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<textarea id="TxtPTypeValue"></textarea>
<button onclick="TransactionType()">Print Value</button>
Upvotes: 0
Reputation: 185
Correct Syntax
$('#TxtPTypeValue').val()
When used to return value:
This method returns the value of the value attribute of the FIRST matched element.
When used to set value:
This method sets the value of the value attribute for ALL matched elements.
Upvotes: 1