function hasPasswordChanged(val...">

How can I convert this working JAVASCRIPT code to JQUERY

 <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>

      

0


a source to share


6 answers


Assuming I'm right if you want to disable navigation links on the page if the password has already been changed (1 is correct).



$(function() {
   var changed = <%= HasPasswordAlreadyChanged %>;
   if (changed) {
      $('#sNav a').attr('disabled','disabled')
                  .click( function() { return false; } );
   }
});

      

+10


a source


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

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

      

or bit wierder:



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

      

+1


a source


<script language="javascript" type="text/javascript">
  $(function(){
    if ('<%  = HasPasswordAlreadyChanged %>' == '1') {
      $("#sNav").find("a").attr("disabled","disabled").click(function(){return false;});
    }
  });
</script>

      

+1


a source


Assuming HasPasswordAlreadyChanged is 0 or 1 (or flase / true)

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

      

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

0


a source


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

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

      

This selects the tags a

that are children of the node with the id sNav

, sets all disabled attributes to true, and sets the callable return function false to trigger the click event.

The last part, the call $()

to the specified function, runs that function when the DOM is ready to go, and is synonymous $(document).ready()

when passed to the function. You can also replace this with your parameter window.onload

, but the call $()

is preferable with jQuery.

0


a source


Since your JavaScript is running, there is probably no value in converting it to jQuery at the risk of introducing any errors.

The only thing I can consider is using jQuery event handling rather than explicitly using window.onload :

function hasPasswordChanged() {
    // unchanged
}

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

      

0


a source







All Articles