0

When I use this code:

<asp:Button runat="server" ID="Block" Text="text"  OnClientClick="return BlockCheck()"   OnClick="BtnBlockClick"  CausesValidation="False"/>


function BlockCheck() {
            if (x) {
                document.getElementById('#ErrorText').text('Error');
                return false;
            }
        return true;
    }

but this for this part : document.getElementById('#ErrorText').text('Error');<br>to return true.

2
  • 1
    What is x? Where is is defined? Commented Dec 6, 2013 at 11:27
  • x is condition that true Commented Dec 6, 2013 at 11:27

1 Answer 1

2

First, you don't need # in getElementById's param: it really should be written like this...

document.getElementById('ErrorText')

With # that'll try to find an element with ID literally equal to '#ErrorText'- that'll fail, so the function will return null. And any attempt to work with null afterwards (calling some methods off it, in particular) will fail with an Error raised.

Second, you seem to mix jQuery functions and JavaScript ones: there's no text method in DOMElements. Strictly speaking, you'll have to check for textContent support, then go with it; if it's not supported, use innerText instead. For example:

var el = document.getElementById('ErrorText');
el['textContent' in el ? 'textContent' : 'innerText'] = 'Error';

Apparently the check's not required if your project doesn't have to support IE8-.

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

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.