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>
a source to share
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; } );
}
});
a source to share
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; }) : "";
});
a source to share
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.
a source to share
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 %>');
});
a source to share