Question

I am trying to implement server side code on client side. "trvddl1" is a ascx control which contains a dropdownlist "ddltree". It was easy on server side but I am facing difficulty using the same in javascript.

How do I write the following code in javascript?

((DropDownList)trvddl1.FindControl("ddltree")).SelectedValue;

I tried

var abc = document.getElementById('<%=trvddl1.ClientID%>').value;

and

var Region = document.getElementById('<%=trvddl1.FindControl("ddltree")%>').value;

but javascript returned error. Is there some other keyword I am missing ?

Was it helpful?

Solution

Check the HTML output (Browser-->View Source) and locate the control there, see what the ID of that control has, and put that one into the getElementById() function.

Example:

<input id='ddltree' .... />

Then use:

var abc = document.getElementById('ddltree').value;

OTHER TIPS

Perhaps you can try something like that:

// find all controls that have an id that ends with ddltree 
// starts with would be [id*=ddltree]
var abc = document.querySelectorAll("[id$=ddltree]");
if(abc.length > 0) { 
   // got it !
   console.log(abc[0].value);
}

Please note that querySelectorAll is not supported in all browsers (even though - most). Here is a reference.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top