views:

694

answers:

6
 <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>
+9  A: 

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; } );
   }
});
tvanfosson
> 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.
Dmitri Farkov
That the trouble with "word for word" translations, instead of "thought for thought" translations. I've improved it (hopefully).
tvanfosson
Seems perfect imo!
Dmitri Farkov
yea you have, but is disabled a proper attribute in html?
TStamper
@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.
tvanfosson
+1 ok just checking
TStamper
Thank you so much that works great!
Shiva
+1  A: 
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; }) : ""; 
});
Dmitri Farkov
I believe you can skip the each and just chain the attr and click functions too.
Tom Hubbard
So true edited..
Dmitri Farkov
Thanks! T d
Dmitri Farkov
+1  A: 
<script language="javascript" type="text/javascript">
  $(function(){
    if ('<%  = HasPasswordAlreadyChanged %>' == '1') {
      $("#sNav").find("a").attr("disabled","disabled").click(function(){return false;});
    }
  });
</script>
jakemcgraw
Thanks I like your approach.
Shiva
So, mark it as the correct answer :P
jakemcgraw
A: 

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 ?

duckyflip
A: 
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.

Rudd Zwolinski
Thank you for solution along with very nice explanation. That is really kind of you!
Shiva
A: 

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 %>');
});
Ian Oxley