<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>
-
1Given that jQuery is JavaScript, I'm having trouble not making the glib response and reposting your exact code as an answer. :PDan Lew– Dan Lew2009-05-22 18:57:54 +00:00Commented 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?jrcs3– jrcs32009-05-22 18:59:06 +00:00Commented May 22, 2009 at 18:59
-
3@Daniel: while all jQuery is Javascript, not all Javascript is jQuery.tvanfosson– tvanfosson2009-05-22 19:01:13 +00:00Commented May 22, 2009 at 19:01
-
1I 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.Shiva– Shiva2009-05-22 19:01:46 +00:00Commented May 22, 2009 at 19:01
-
@Daniel - no offense intended.tvanfosson– tvanfosson2009-05-22 19:08:44 +00:00Commented May 22, 2009 at 19:08
6 Answers
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; } );
}
});
3 Comments
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
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
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 %>');
});