Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

LinkItem onclick event handling

Status
Not open for further replies.

kenpogi

Programmer
Jun 30, 2004
11
US
Hi.
I'm having a problem regarding access of the onclick event of a dropdownlist in javascript. It seems that during the onclick event, the javascript cannot catch the SelectedIndex that I chose..rather..it always defaults to the 0 index:

Here's the code:

<script language='javascript'>
<!--
if(document.getElementById('Contentcontainer1__ctl0_Region3_ucCurrentFilledRoles_dgSRP1__ctl2_NeedTo0')!=null){
document.getElementById('Contentcontainer1__ctl0_Region3_ucCurrentFilledRoles_dgSRP1__ctl2_NeedTo0').onclick = Contentcontainer1__ctl0_Region3_ucCurrentFilledRoles_dgSRP1__ctl2_NeedTo0_OnClick;}else{}
function Contentcontainer1__ctl0_Region3_ucCurrentFilledRoles_dgSRP1__ctl2_NeedTo0_OnClick(){
var returnValue = null;
var strListItem = '2|3';
var arrListItem = strListItem.split('|');
var selectedIndex = document.getElementById('Contentcontainer1__ctl0_Region3_ucCurrentFilledRoles_dgSRP1__ctl2_NeedTo0').selectedIndex;
alert(selectedIndex);var bFound = false;
for (var i=0; i < arrListItem.length; i++){
if (selectedIndex == arrListItem){
bFound = true;
break;}}alert(bFound);
if (bFound){
if(document.getElementById('Contentcontainer1__ctl0_Region3_ucCurrentFilledRoles_txtChange')!=null){
if(document.all('Contentcontainer1__ctl0_Region3_txtChange').value=='changed'||document.all('Contentcontainer1__ctl0_Region3_ucCurrentFilledRoles_txtChange').value!=''){
returnValue = confirm('Your updates have not been saved. Click OK to continue going to the previous page, CANCEL to retain current page.')}}
else{
if(document.all('Contentcontainer1__ctl0_Region3_txtChange').value=='changed'){
returnValue = confirm('Your updates have not been saved. Click OK to continue going to the previous page, CANCEL to retain current page.')}}}}
document.all('Contentcontainer1__ctl0_Region3_ReturntxtChange').value = returnValue;
if(returnValue==null){}else{
if(returnValue){}else{return false;}}
}
//-->
</script>

Thanks for the help
 

I'd suggest using "onchange" instead of "onclick".

I'd also suggest tidying up your code a bit, and I'd start by assigning this:

Code:
document.getElementById('Contentcontainer1__ctl0_Region3_ucCurrentFilledRoles_dgSRP1__ctl2_NeedTo0');

to a variable right at the top of your code. That way, you can just use a (consderably shorter) method of accessing the element.

Hope this helps,
Dan


 
Will try the onchange event. Thanks.
As for the client id, the javascript's dynamically formed that's why it's that long :)
 

>> As for the client id, the javascript's dynamically formed that's why it's that long

Just because it's dynamically generated, does not mean that you cannot shorten it as I suggested. It is still a very simple task to do, with (IMHO) great benefits (code readability, less bytes download, less overheads when resolving the object... should I go on? ;o)

Dan
 
ok..code seems to be working for onchange..
one last question..how do i fire the onclick event inside the onchange function?is that possible?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top