0
 <script language="javascript" type="text/javascript">


function hasPasswordChanged(value)
  {
        if(value == '1')
        {
            var container = document.getElementById("sNav");
            if(document.getElementsByTagName)
            {
               var hyperLinkList = container.getElementsByTagName("a");

               for(var currentLink in hyperLinkList)
               {
                    hyperLinkList[currentLink].disabled = true;
                    hyperLinkList[currentLink].onclick =function () { return false;}

               }

            }
        }
  }


  window.onload = function () 
  {
        hasPasswordChanged('<%  = HasPasswordAlreadyChanged %>');
  }

</script>
6
  • 1
    Given that jQuery is JavaScript, I'm having trouble not making the glib response and reposting your exact code as an answer. :P Commented May 22, 2009 at 18:57
  • Why do you need to do this? What jquery features do you need to use? What is your objective? ETC? Commented May 22, 2009 at 18:59
  • 3
    @Daniel: while all jQuery is Javascript, not all Javascript is jQuery. Commented May 22, 2009 at 19:01
  • 1
    I am just curious how elegent and compact code might be with JQUERY. Of course, this code is working fine --- does what it needs to be done; of course I have tested this code on any other browser beside IE . Thanks. Commented May 22, 2009 at 19:01
  • @Daniel - no offense intended. Commented May 22, 2009 at 19:08

6 Answers 6

10

Assuming I'm correct in that you want to disable the nav links on the page if the password has already changed (1 is true).

$(function() {
   var changed = <%= HasPasswordAlreadyChanged %>;
   if (changed) {
      $('#sNav a').attr('disabled','disabled')
                  .click( function() { return false; } );
   }
});
Sign up to request clarification or add additional context in comments.

3 Comments

> Selects immediate children... Meanwhile getElementsByTagName selects descendants of a parent node. Also, you can set disabled using the .attr function. Small nitpicks, just thought i'd share.
yea you have, but is disabled a proper attribute in html?
@TStamper - not for an anchor -- perhaps, that's the reason for also adding a click handler that returns false -- for browsers that don't support disabling an anchor.
1
<script language="javascript" type="text/javascript">
  $(function(){
    if ('<%  = HasPasswordAlreadyChanged %>' == '1') {
      $("#sNav").find("a").attr("disabled","disabled").click(function(){return false;});
    }
  });
</script>

Comments

1
function hasPasswordChanged(value)
  {
        if(value == '1')
        {
            $('#sNav a').attr('disabled', 'true').click(function(){ return false; });
        }
  }

$(function(){
    hasPasswordChanged('<%  = HasPasswordAlreadyChanged %>');
})

or a bit wierder:

$(function(){
    <%  = HasPasswordAlreadyChanged %> == 1 ? $('#sNav a').attr('disabled', 'true').click(function(){ return false; }) : ""; 
});

Comments

0

Presuming HasPasswordAlreadyChanged is either 0 or 1 (or flase/true)

jQuery(function($){
  !!<%= HasPasswordAlreadyChanged %> && $("#sNav a").attr("disabled",true).click(function(){return false;})
})

Also, does a disabled attribute on A element affect it in any way ?

Comments

0
function hasPassWordChanged(value) {
  if (value == '1') {
    $("#sNav a").attr("disabled", true).click(function() {return false;});
  }
}

$(function() {
  hasPasswordChanged('<%  = HasPasswordAlreadyChanged %');
});

This selects all a tags that are children of the node with id sNav, sets all of their disabled attributes to true, and sets the specified returning false function to be called upon the click event.

The last part, a call to $() with the specified function, runs the function when the DOM is ready to be worked on, and is a synonym for $(document).ready() when passed a function. You can also substitute this with your window.onload setting, but the call to $() is more preferred with jQuery.

1 Comment

Thank you for solution along with very nice explanation. That is really kind of you!
0

As your JavaScript is working, there is probably no value in converting it to jQuery at the risk of introducing any bugs.

The only thing I might consider is using jQuery's event-handling rather than explicitly using window.onload :

function hasPasswordChanged() {
    // unchanged
}

$(document).ready(function() {
    hasPasswordChanged('<%  = HasPasswordAlreadyChanged %>');
});

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.