document.getElementById(), error in content page content page

  • Thread starter Thread starter Dave L
  • Start date Start date
D

Dave L

Could someone explain: I have a java popup window function that I would
like to pass a querystring ID from a hiddenfield on my asp.net content page.
The following code works. Note: for this test I simply set the hiddefield
var TransID.value = 21 in btnPopUp.click event See Item 1. The thing I
don't understand is why can't I have the function take the TransID as a
parameter. see Item 2.


**************Item 1.****************

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">

<script type="text/javascript" >
function PopUp() {

var InqID = document.getElementById('<%=TransID.ClientID%>').value;
alert(InqID);
}

</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1"
Runat="Server">
<asp:Button ID="btnPopUp" runat="server" Text="Press Me"
OnClientClick="javascript:PopUp();" />

<asp:HiddenField ID="TransID" runat="server" />
</asp:Content>



*************Item 2.****************

This Fails

***********************************


<script type="text/javascript" >
function PopUp(obj) {

var InqID = document.getElementById(obj).value;
alert(InqID);
}

</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1"
Runat="Server">
<asp:Button ID="btnPopUp" runat="server" Text="Press Me"
OnClientClick="javascript:PopUp('<%=TransID.ClientID%>');" />

<asp:HiddenField ID="TransID" runat="server" />
</asp:Content>


Any help would be greatly appreaciated.

Feel free to email me at (e-mail address removed)
 
Dave said:
Could someone explain: I have a java popup window function that I would
like to pass a querystring ID from a hiddenfield on my asp.net content
page. The following code works. Note: for this test I simply set the
hiddefield var TransID.value = 21 in btnPopUp.click event See Item 1.
The thing I don't understand is why can't I have the function take the
TransID as a parameter. see Item 2.

<script type="text/javascript" >
function PopUp(obj) {

var InqID = document.getElementById(obj).value;
alert(InqID);
}

</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1"
Runat="Server">
<asp:Button ID="btnPopUp" runat="server" Text="Press Me"
OnClientClick="javascript:PopUp('<%=TransID.ClientID%>');" />

<asp:HiddenField ID="TransID" runat="server" />
</asp:Content>
------------------------------------------------

Didn't you pass the control as an object to the function?

Maybe, I am wrong, but the object has the data and you don't need to do
getEbyID in the function, because you passed the control to the function.

var InqID = object.value;

But first do an alert(obj) to even see if you have an object.
 
Mr. Arnold said:
<script type="text/javascript" >
function PopUp(obj) {

var InqID = document.getElementById(obj).value;
alert(InqID);
}

</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1"
Runat="Server">
<asp:Button ID="btnPopUp" runat="server" Text="Press Me"
OnClientClick="javascript:PopUp('<%=TransID.ClientID%>');" />

<asp:HiddenField ID="TransID" runat="server" />
</asp:Content>
------------------------------------------------

Didn't you pass the control as an object to the function?

Maybe, I am wrong, but the object has the data and you don't need to do
getEbyID in the function, because you passed the control to the function.

var InqID = object.value;

But first do an alert(obj) to even see if you have an object.

No, it's not an object, it's a string. Note the apostrophes around the
value in the function call.
 
Dave said:
Could someone explain: I have a java popup window function that I would
like to pass a querystring ID from a hiddenfield on my asp.net content
page. The following code works. Note: for this test I simply set the
hiddefield var TransID.value = 21 in btnPopUp.click event See Item 1.
The thing I don't understand is why can't I have the function take the
TransID as a parameter. see Item 2.


**************Item 1.****************

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">

<script type="text/javascript" >
function PopUp() {

var InqID = document.getElementById('<%=TransID.ClientID%>').value;
alert(InqID);
}

</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1"
Runat="Server">
<asp:Button ID="btnPopUp" runat="server" Text="Press Me"
OnClientClick="javascript:PopUp();" />

<asp:HiddenField ID="TransID" runat="server" />
</asp:Content>



*************Item 2.****************

This Fails

***********************************


<script type="text/javascript" >
function PopUp(obj) {

var InqID = document.getElementById(obj).value;
alert(InqID);
}

</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1"
Runat="Server">
<asp:Button ID="btnPopUp" runat="server" Text="Press Me"
OnClientClick="javascript:PopUp('<%=TransID.ClientID%>');" />

<asp:HiddenField ID="TransID" runat="server" />
</asp:Content>


Any help would be greatly appreaciated.

Feel free to email me at (e-mail address removed)

The problem is that you can't have a script tag inside the Button tag.

The easiest is to set the OnClientClick value from code behind:

btnPopUp.OnClientClick = "PopUp('" + TransID.ClientID + "')";

(Note that the javascript: protocol is only used when you put script in
an URL, e.g. in the href attribute of a link. If you use it in the
onclick attribute, it will instead be interpreted as a label.)
 
Back
Top