You need to sign in to do that
Don't have an account?
Problem with document.getElementbyID
Hi i am unable to get the selected radio button value into the javascript . I am getting as undefined .
i want the value when i am clicking another button in the same form.
<apex:page controller="sampleCon" sidebar="false" showHeader="false" id="testpage">
<script>
function checkThis(){
alert('hi');
var a=document.getElementById('{!$Component.theform.country}').value
alert(a);
}
</script>
<apex:form id="theform">
<apex:selectRadio value="{!country}" id="country" >
<apex:selectOptions value="{!items}"/>
</apex:selectRadio><p/>
<apex:commandButton value="Test" action="" onclick="checkThis();" rerender="out" status="status"/>
</apex:form>
</apex:page>
<apex:outputPanel id="out">
<apex:actionstatus id="status" startText="testing...">
<apex:facet name="stop">
<apex:outputPanel >
<p>You have selected:</p>
<apex:outputText value="{!country}"/>
</apex:outputPanel>
</apex:facet>
</apex:actionstatus>
</apex:outputPanel>
</apex:page>
=============================
Controller :
public class sampleCon {
String country = null;
public PageReference test() {
return null;
}
public List<SelectOption> getItems() {
List<SelectOption> options = new List<SelectOption>();
options.add(new SelectOption('US','US'));
options.add(new SelectOption('CANADA','Canada'));
options.add(new SelectOption('MEXICO','Mexico')); return options;
}
public String getCountry() {
return country;
}
public void setCountry(String country) { this.country = country; }
}
Hi
here I am pasting the page code of yours with a small tweak.
If you observe the value resulted for alert(a1) and alert(a); , you may find object:HTML table element for the first alert.
This means that the selectradio component is decoded to HTML table element. That's the reason you are not able to get the value of the decoded table element.
And you will find the proper value for second alert. This is due to the onchange event. On change of the radio button value it will fetch the value of the selected radio button's value.
All Answers
Hi
here I am pasting the page code of yours with a small tweak.
If you observe the value resulted for alert(a1) and alert(a); , you may find object:HTML table element for the first alert.
This means that the selectradio component is decoded to HTML table element. That's the reason you are not able to get the value of the decoded table element.
And you will find the proper value for second alert. This is due to the onchange event. On change of the radio button value it will fetch the value of the selected radio button's value.
Harsh...........Superb Man... I am breaking my head from morning onwards...
Thanks alot dude... Its working fine..:-)
Harsha.. One more problem here..In onother case i need to set radio button value from some another javascript method .
I am passing a JSON value from controller to page . As long as the page is loaded i need to pasre the JSON and i need to set those values to the corresponding VF tags. If the radio button is a div tag then with Jquery or document.getElementbyId we can set the value directly . But it is VF selectOptions . So how can i set the value .
Assume that in getJson() we have that value . we need to set that to selectOptions radio tag . Please Check the below code and help me how to acheive . I dont want to bind the values from constructor to the appropriate tag. This should happen through the JS method of a page .
Hi Prakash,
add this to your script and check!
Give KUDOs if the posts are helping you!
Hi hasha,
It sound good .
But multiple radio button section are there in my page so each time if use document.getElementsByTagName('label'); for which section this value will go . For example there are two sections country (radio) and State(radio) like that 4 sections are there . i got all the values in Javascript i need to pass these values to the corresponding tags. This is my original scnerio . I am looking for document.getElementById('{!$Component.theform.country}'); this type of scnerio but it seems there no way to set like this . Those sections were keep on increasing or decreasing in the page . Do you have any idea to set these values for multiple sections accordingly.
Ohh, Ok I got you.
This can be resolved by doing this.
Change this as followed
By this you can be able to fetch the labels of perticular selectRadio component.
I believe you have different ids for different selectRadio components.
It's Perfect Now. Thanks alot harsha
Adding to that we can try this way also .