Solved

use javascript to assign new href to button in .net app

Posted on 2014-01-28
5
554 Views
Last Modified: 2014-01-28
I'm working with a .net app and I need to re-assign the href attribute in a button. I can't mess with the .net code, but I can add all the javascript I want. However, so far I've had no luck.

Here's the button:

<a name="ctl00$ctl00$btnSubmit" language="javascript" href="javascript:{ClickOnce_ctl00_ctl00_btnSubmit();}" id="ctl00_ctl00_btnSubmit" style="float:left;">Save</a>

Here's how I'm trying to assign something new to href:

<script language="javascript">
 $(document).ready(function() {
 $("#ctl00_ctl00_btnSubmit").href="javascript:{alert('You clicked save!');}"
 }
 </script>

But nothing doing. Doesn't work. Can somebody help? Is there another method?
0
Comment
Question by:GessWurker
  • 3
  • 2
5 Comments
 
LVL 33

Expert Comment

by:Big Monty
ID: 39816178
you're better off binding to the click() event of the button

<script language="javascript">
 $(document).ready(function() {
 $('#ctl00_ctl00_btnSubmit').on('click', function () {
            alert('You clicked save!');
  });
 }
 </script>
0
 
LVL 33

Expert Comment

by:Big Monty
ID: 39816187
if that doesn't work, then you can change the href like this:

$("#ctl00_ctl00_btnSubmit").attr("href", "javascript:{alert('You clicked save!") )
0
 

Author Comment

by:GessWurker
ID: 39816347
Hi Big Monty.

Neither option worked. No alert.
0
 
LVL 33

Accepted Solution

by:
Big Monty earned 500 total points
ID: 39816375
try this, fixed a few typos:

<script language="javascript">
    $(document).ready(function () {
        $('#ctl00_ctl00_btnSubmit').attr("href", "javascript:{ alert('You clicked save!') }");
    });
 </script>
0
 

Author Comment

by:GessWurker
ID: 39816384
Excellent. Works!

Thanks!
0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question