1

I have hidden field to store the value and bind the value in javascript aswell, and now it pops a problem as my server doesnt support 4.0 either its in 3.5 so it doesnt support ClientIDmode property , please find code below

<asp:HiddenField ID ="lbl1" ClientIDMode="Static" runat="server"></asp:HiddenField>

Javascript:

function sourceval() {
         var src1 = window.document.getElementById('lbl1').value;           
    }

if i use literal it has visibility property where the rendering of the page would be problem , please suggest.

0

2 Answers 2

2

You don't necessarily need the ClientIDMode, you can simply access the element by its generated ClientID:

function sourceval() {
    var src1 = window.document.getElementById('<%= lbl1.ClientID %>').value;           
}
Sign up to request clarification or add additional context in comments.

Comments

1

The DOM element ID will not be the same as the server control ID if the ClientIDMode is not static, which is a feature debuted in .NET 4. I think this is happening here. The proposed solutions are...

  1. If your Javascript is written on the same ASPX/ASCX

    var src1=document.getElementById('<%= lbl1.ClientID%>');

  2. If you are on external Javascript, then add a class to the hidden field and use JQuery to select by class

    var src1=$('.youHiddenFieldClass').val(); or in plain Javascript like

    document.getElementsByClassName('classname')

1 Comment

The result of #1 is an element reference, the result of #2 is a string value. Given that the W3C Selector API is supported by pretty much all browsers in use and the OP has not included a jQuery tag, it would be better to propose a non–jQuery answer, like: document.querySelectorAll('.youHiddenFieldClass')[0].value. However, that should be feature tested first.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.