JQuery cannot "connect" to elements in my server generated web page
I am trying to use jQuery functionality in Software AG webMethods IDE which generates pages itself. Basically, I provide jQuery functions with an element id on the server, but jQuery can't read its value, or write a value to it, or do something with it.
Here is the code I'm using:
if(jQuery.isNumeric(jQuery(CAF.model("#{activePageBean.clientIds['txtInput']}").id).val()))
{
alert("its a number");
}
else
{
alert("its not a number");
}
txtInput is a normal text input box and the above code is assigned to the button ... And no matter what is put in the text box, I always get a "its not a number" warning.
Also, if I use this code instead.
alert(jQuery(CAF.model("#{activePageBean.clientIds['txtInput']}").id).val());
... I always get an "undefined" warning (no double quotes).
Expression returning the ID of the text box:
CAF.model("#{activePageBean.clientIds['txtInput']}").id
.. and this:
jsfwmp147312:defaultForm:txtInput
If I run this built-in webMethods function instead to get the value:
CAF.model("#{activePageBean.clientIds['txtInput']}").getValue();
It returns the current value in the text box.
So, for some reason jQuery functions are unable to "connect" to page elements.
How do I diagnose / resolve this?
EDIT:
Additional Information:
If I try the following code ...
var myId = "jsfwmp147312:defaultForm:txtInput";
var element = document.getElementById(myId);
alert(element.value + "");
.. I get the string value of the string in the alert, no problem ... But if I try this ...
var myId = "jsfwmp147312:defaultForm:txtInput";
var val2 = jQuery(myId).val();
alert(val2);
... I get a warning with the message "undefined" (no double quotes).
source to share
We need some HTML code. However, I don't understand your selector. You say it is "assigned to a button", so you'll need it click
.
jQuery(document).on("click", "#clientIds[name=txtInput]", function(){
v = $(this).val();
alert(v);
});
In general, client side code and server side code are not communicated. The server code spits out the HTML, and the client code processes it later. To exchange information, you need to send data through, for example, pages or AJAX.
source to share
Problem: means something in the jQuery selector so it doesn't look for something else.
The line should look like jsfwmp147312\:defaultForm\:txtInput
So, you need to make it work in two steps
var myId = CAF.model("#{activePageBean.clientIds['txtInput']}").id.replace(/:/g,"\\:");
var val = jQuery(myId).val();
if (jQuery.isNumeric( val ) {
alert("its a number");
} else {
alert("its not a number");
}
source to share