2

I'm using ASP.NET and in the code behind I need to add an attribute which makes text2 appear when the user puts the mouse over text1; below is what I currently have.

JS:

function show(name) {
        document.getElementById(name).visible = true;
    }

C#:

text2.Attributes.Add("onmouseover", "show(" + text2.ClientID +")");

HTML:

<asp:HyperLink runat="server" ID="text2" Visible="false"/>
<asp:HyperLink runat="server" ID="text1" rel="external" />
2
  • is there any error you are facing? Commented Feb 12, 2016 at 16:26
  • Make sure you don't confuse asp.net server properties with css properties. visibility=true is an asp.net server property, which is resolved before the page is rendered. javascript has no knowledge of these properties Commented Feb 12, 2016 at 16:41

3 Answers 3

3

Two things here:

  1. Visible="false" is not a styling, it is a server-side property. Setting it false means that the control won't be rendered at all. So it won't even exist on the client side, and of course there is no way to access it via javascript. Instead consider assigning a CSS class to it or, at the very least, inline style like style="visibility: hidden"

  2. You want to wrap the id of the control into quotes. Say if your control ends up with id "blah_blah_text2", your javascript will look like

    show(blah_blah_text2);
    

    so js will try to evaluate "blah_blah_text2" as a variable, and will fail of course. Instead you want

    show('blah_blah_text2');
    

    so

    "show('" + text2.ClientID +"')"
    

Also I am not quite sure what visible = true; stands for. There is no such property in DOM objects as far as I know. Did you actually mean document.getElementById(name).style.visibility = "visible"?

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

2 Comments

inline style like style="visible: false" is wrong. you've to use either "display:none;" or "visibility:hidden;". the second one will leave the placeholder as it is.
Also note his javascript need to be amended to ...document.getElementById(name).style.visibility = 'visible';...
0

which makes text2 appear when the user puts the mouse over text1; below is what I currently have.

Html title attribute serves this purpose.

Try this:

text2.Attributes.Add("title", text2.ClientID);

Comments

0

Hope You are looking for this:

text2.Attributes.Add("onmouseover", "show('" + text2.ClientID +"')");

Comments

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.