I have one string name as id in my .aspx.cs page and one javaScript function SetValue(val) on .aspx page in which I have to assign val to that id. How will I do this in java script fuction SetVal(val)?
+1
A:
If I've understood you correctly, I would have thought that you would need something like the following
function SetValue(elem, val) {
elem.value = val;
}
and then use like so
var element = document.getElementById('id_of_element');
SetValue(element, 'Value to Set');
EDIT:
if you wanted to make the SetValue function specific to only setting the value of one element (which I wouldn't really recommend - it would be better to write a generic function rather than tie the function to one specific element), you could do the following
function SetValue(val) {
var elem = document.getElementById('id_of_element');
elem.value = val;
}
Russ Cam
2009-09-02 10:58:57
of course, this will need to load when the DOM has loaded and the id_of_element will need to be the ClientID of the control if it is a server control and is a child of any other class that implements INamingContainer
Russ Cam
2009-09-02 12:20:52
+1
A:
Implement a protected class variable in your .aspx.cs-file and reference that variable - inside your JavaScript function - in your .aspx-file:
.aspx.cs:
public partial class X : System.Web.UI.Page
{
protected String useMeInJavaScript = "";
}
.aspx:
<script>
SetValue(<%= useMeInJavaScript %>)
</script>
Note: this is pretty simplified, but I'm sure you get the point.
roosteronacid
2009-09-02 11:53:54
Glad I could help. Now; to complete the circle--please accept my answer :)
roosteronacid
2009-10-26 23:48:39