You need to sign in to do that
Don't have an account?
jungleee
getting input value from jQuery
Hi,
I have created a input box and a link. When I click on the link, an alert will show the value of the input box. But this is not happening as of now, Please suggest the code changes. Thanks!
<apex:page id="pa"> <apex:includeScript value="{!$Resource.jQuery}"/> <script type="text/javascript"> var j$ = jQuery.noConflict(); j$(document).ready(function(){ j$("a").click(function(){ var xyz = j$("#pa:f1:userInput").val(); //var xyz = document.getElementById('{!$Component.pa.f1.userInput}').value; alert(xyz); }); }); </script> <a herf="#" id="clickHere">click here</a> <apex:form id="f1"> enter something <apex:inputText id="userInput"/> </apex:form> </apex:page>
Thanks
Sam
Sorry, I read your code wrong. It looks like you are using an apex input field which is a little different in targeting elements...here's how your code should look
I'd recommend staying away from apex elements since you really don't need them with jQuery, especially in the example you gave. A better option would be...
You should also leverage DIVs to organize your elements. It will make your life so much easier with jQuery.
All Answers
You need to replace double quotes with single quotes since this is visualforce...is a an element? you should have
and
Hope this helps!
Hi ,
Thanks for the reply.
I tried replacing the double qoutes with single, but that didn't work out. But the below code works. When I click on the link, I get a hello alert.
I think there's somthing wrong with getting the value of the input and storing it in the xyz variable. i.e., with the below line:
Any help here, please?
-Sameer
Why don't you just use the input's element name?
var xyz=j$('#userinput').val();
Hi,
I tried that as well, but when I clicked on the link, i get the pop-up but with undefined on it..
Not sure how to resolve this..
-Sam
Could you post the code of the elements you are trying to refrence? Including the divs they are in
Hi
This is the below code:
I used both single and double quotes, but it didn't work.
If I use the below javascript line everything works fine:
var xyz = document.getElementById('pa:f1:userInput').value;
I guess something's wrong with the jQuery code.
-Sam
Sorry, I read your code wrong. It looks like you are using an apex input field which is a little different in targeting elements...here's how your code should look
I'd recommend staying away from apex elements since you really don't need them with jQuery, especially in the example you gave. A better option would be...
You should also leverage DIVs to organize your elements. It will make your life so much easier with jQuery.
Thanks mate, that worked!!!
I will surely use the div's going ahead..
Thanks again
-Sam
Hi,
I have similar issue, <apex:inputText> is not working in my case too, i have used <input> instead and its working as expected. My question is how to send the value into to the controller to save the record. Could you please help on this ?
here is my code:
<apex:page >
<link rel="stylesheet" href="http://code.jquery.com/ui/1.9.0/themes/base/jquery-ui.css" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.9.0/jquery-ui.js"></script>
<script type="text/javascript">
$(function () {
$("#datepicker").datepicker({ beforeShowDay: allowedDates });
});
var currentYear = new Date().getFullYear();
var _allowedDates = [
new Date(currentYear, 2, 31).getTime(),
new Date(currentYear, 5, 30).getTime(),
new Date(currentYear, 8, 30).getTime(),
new Date(currentYear, 11, 31).getTime(),
];
function allowedDates(date) {
date = date.getTime();
for (i in _allowedDates)
if (date == _allowedDates[i])
return [true, ""];
return [false, ""];
}
</script>
<apex:form >
<! <apex:inputText value="{!d_date_str}" id="datepicker" /> -->
<input type="text" id="datepicker" />
</apex:form>
</apex:page>
Any help is highly appreacited.
Thanks inadvance!
SK
This works.