If you're simply acting on an element based on its visibility, just include ":visible" or ":hidden" inthe selector expression. For example:
$('#myDiv:visible').animate({left: '+=200px'}, 'slow');
How do I select an element that has weird characters in its ID?
For example, it can be common for some frameworks to generate unique IDs that have specialcharacters in them (like '.' or '[..]'). The problem is that these characters have a special meaning inCSS.Thankfully, jQuery has a workaround, which allows you to do the following:
// Does not work$("#some.id")// Works!$("#some\\.id")
and another example:
// Does not work$("#some[id]")// Works!$("#some\\[id\\]")
A convenient way to wrap some wrapping is the following function:
function jq(myid){ return '#'+myid.replace(/:/g,"\\:").replace(/\./g,"\\.");}
this allows to use "just the ID" to identify a DOM-Element. jq(..) takes care of adding a '#' at the beginning and escaping all dots.[edit]
How do I disable/enable an element?
You can disable/enable an element by setting the 'disabled' attribute to 'disabled' (to disable it) or "" (to enable it). The result of which looks something like this:
// Disable #x$("#x").attr("disabled","disabled");// Enable #x$("#x").removeAttr("disabled");
You can try an example of enabling/disabling with the following demo:and here's the source code to the demo:
<select id="x" style="width:200px;"><option>one</option><option>two</option></select><input type="button" value="Disable"onclick="$('#x').attr('disabled','disabled')"/><input type="button" value="Enable" onclick="$('#x').removeAttr('disabled')"/>
Leave a Comment