0

I need to use an OnFocus event of a textbox in ASP.Net.

 <asp:TextBox ID="TBAccountNum" runat="server" CssClass="textbox" Height="12px" Width="100px" Font-Size="Small" AutoCompleteType="None"  Wrap="False" OnFocus="Validator()" OnTextChanged="TBAccountNum_OnLeave" AutoPostBack="true"></asp:TextBox>

The only way to do that is to use Javascript, which I know nothing about. I found this little piece of code on SO that looks like it should work:

<asp:Content runat="server" ID="FeaturedContent" ContentPlaceHolderID="FeaturedContent">

<script type="text/javascript">

    function Validator()
    {
        document.getElementsByName("btnValidateWork").style.visibility = "visible"; 
        document.getElementsByName("btnSubmitWork").style.visibility = "hidden"
    }

However, when I run it I get the following error:

0x800a138f - Microsoft JScript runtime error: Unable to set value of the property 'visibility': object is null or undefined

Any ideas what I'm doing wrong (besides trying to write code I know nothing about... :oP )?

All relevant code has now been posted. I'm using IE9 on an Intranet (mandated by the company, so that can't change) if that matters.

2 Answers 2

1

document.getElementsByName("btnValidateWork") returns an htmlcollection. You have to iterate it, and for each item (node) add the proper css.

Check out this: Using document.getElementsByName() isn't working?

EDIT:

to use getElementsByName, your element(s) must have a name property. Example:

<button name="btnValidateWork">my button?</button>
document.getElementsByName("btnValidateWork")[0].style.visibility = "hidden";

Fiddle: http://jsfiddle.net/chrisbenseler/h3dwwud6/

I think that a better approach is to use getElementsByClassName and add some class to it, and then do something like document.getElementsByClassName("hidden")

Sign up to request clarification or add additional context in comments.

7 Comments

Should I have used GetElementsByID? Any chance you can post the code that will actually work for me?
@JohnnyBones I edit my answer, with some code. Don't have to use ids, but you can. You can also use a class.
Now it's throwing this error: 0x800a138f - Microsoft JScript runtime error: Unable to get value of the property 'style': object is null or undefined
Post some code (jsfiddle, codepen, etc...) so we can see what is happening.
Post the markup/source of the html that your asp tag generates. document.getElementsByName("btnValidateWork") is returning null (or undefined).
|
0

As said before document.getElementsByName returns a collection (array) of HTML-Objects. So you need to address them like this:

 function Validate()
    {
        // To access the first
        document.getElementsByName("btnValidateWork")[0].style.visibility = "visible"; 
        document.getElementsByName("btnSubmitWork")[0].style.visibility = "hidden"
        // To access all of them
       var i = 0,
           validWork = document.getElementsByName("btnValidateWork");
       for (i = 0; i < validWork.length; i++) {
           validWork[i].style.visibility = "visible";
           // ...
       }
    }

See a working example here.

5 Comments

See the error I posted to Chris' question. Same thing.
From the JavaScript part, it should work! the error must be somewhere else
The second part doesn't error, but it also doesn't do anything. I'm usine IE9, does that matter?
Please provide a jsfiddle or similar
@JohnnyBones Sry man. it works. See here: jsfiddle.net/danielgasser/r3f5se5n

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.