Avatar of Wigging
Wigging
Flag for United States of America asked on

call javascript on button click

i have this url below
   <a href="javascript:fnSelect('test1');"> Select Signature</a>

how can i do the same but by submitting asp button control?
Visual Basic.NETASP.NET

Avatar of undefined
Last Comment
Roopesh Reddy

8/22/2022 - Mon
Jorge Paulino

You can do something like this:

<asp:button runat="server" text="Test" OnClientClick="javascript:fnSelect('test1');" id="Button1"></asp:button>
Wigging

ASKER
when I do as a link the selection stays selected........but when i do it as a button, it doesnt stay selected.....i believe because of postback

I have this script

<script type="text/javascript">
    function fnSelect(objId) {
        fnDeSelect();
        if (document.selection) {
            var range = document.body.createTextRange();
            range.moveToElementText(document.getElementById(objId));
            range.select();
        }
        else if (window.getSelection) {
            var range = document.createRange();
            range.selectNode(document.getElementById(objId));
            window.getSelection().addRange(range);
        }
    }

    function fnDeSelect() {
        if (document.selection) document.selection.empty();
        else if (window.getSelection)
            window.getSelection().removeAllRanges();
    }
      </script>
Jorge Paulino

Yes, it's because of the postback.

Can you use jQuery?

    $("#button1").click(function () {
        fnSelect('test1');
     });
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Wigging

ASKER
where do i add that code?
Jorge Paulino

ASKER CERTIFIED SOLUTION
Roopesh Reddy

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.