Showing posts with label DHTML. Show all posts
Showing posts with label DHTML. Show all posts

Friday, April 8, 2011

Disable DIV with jQuery

Here's a lame trick I use to disable divs:
// this works on IE // disable $('#myDiv').attr('disabled', 'disabled'); // enable $('#myDiv').attr('disabled', '');

Disabling the whole div doesn't seem to disable inputs in chrome and firefox though, so here's the alternative version drilling down to each single input elements:
// this works everywhere but looks weird in IE // disable $('#myDiv :input').attr('disabled', 'disabled');  // enable $('#myDiv :input').attr('disabled', '');

Good hacking.

Wednesday, February 24, 2010

Falling in line to the (micro)templating frenzy

A few weeks ago I would've laughed in your face if you told me I was gonna fall in line to the templating frenzy that seems to be spreading like a virus between coders. Now - one way or the other - I seem to be infected.

I recently tackled the challenge of generating a DTO layer (including mapping logic) from WCF service client using T4 (with no editor whatsoever - don't get me started). I honestly thought I was gonna blog about that sooner or later but then I immediately got shuffled around like a puppet to some front-end work (I am talking web here) and faced the lame-ass ordinary challenge of dynamically injecting repetitive structures into a given page in response to a given event (click-click-click-click ... click).

Coming from that T4 work, templating obviously came to mind as a way not to get bored: wouldn't it be great if there was something like T4 for js? It sounded crazy at first, but I started looking into it and immediately found the John Resig Micro-Templating engine.

There was no way I was gonna pass on that and, to be honest, the only alternatives were pretty lame:
  • implement your own templating engine 
  • shamelessly hard-code the markup to inject in your .js functions (as I always did before)
So I started playing with it and managed to stumble upon the Rick Strahl variation to it, which actually uses T4 syntax (it sure doesn't look like a coincidence) and also has a nice addition for error handling.

Anyway, this is getting too long: here's an example where I am adding divs with a bunch of input fields to a container, ids are generated at runtime depending on how many divs we have in there. It's ugly as fuck it gets, but it drags the message across (I think).

Let's start
you need to shove the templating engine function in a file. I called it templating.js and you can just copy paste whatever Rick Strahl has on his article. Once you've done that, add templating.js and jQuery.js as external script files to your html.

Once you have that in place, it's time to populate you jQuery init function and add your micro-template (the micro-template is added in a script element defined as text). This should be pretty straightforward if you read the comments:
<!-- all this goes into the head section --> <script type="text/javascript"> $(document).ready(function() { //IDs for the first div var idsArray = { divId: "div_0", input1Id: "input1_0", input2Id: "input2_0" }; // logic to add the first div function onLoad() { var templ = $("#myRepeaterTemplate").html(); var parsed = parseTemplate(templ, idsArray); $("#myTarget").html(parsed); } // inject the first div onLoad(); // Add onclick handler to button w/id addBtn $("#addBtn").click(function() { //1. count how many divs var size = $("#myTarget > div").size(); //2. generate name value pairs var myArray = { divId: "div_" + size, input1Id: "input1_" + size, input2Id: "input2_" + size }; //3. invoke parseTemplate var templ = $("#myRepeaterTemplate").html(); var parsed = parseTemplate(templ, myArray); //4. append $("#myTarget").append(parsed); }); }); </script> <script id="myRepeaterTemplate" type="text/html"> <div id="<#= divId #>" > <input id="<#= input1Id #>" type="text" value="some input" /> <input id="<#= input2Id #>" type="text" value="some input" /> </div> </script>

And the following is what you need in the body of the page for this to work:

<div id="myTarget">
   <p>this stuff should be wiped on load</p>
</div>
<input id="addBtn" type="button" value="Add" />

This is just a very basic example that should be suitable when you just want to inject some markup given a template, but you can put actual js logic into the template. For a nice example of that have a look at this nice example here.

I am a lazy-ass late adopter, and if I am using this stuff  (talking about the templating frenzy in general) - it generally means it can't be ignored much longer. Do so at your own risk.

Saturday, October 25, 2008

[Blogger] How to dinamically change Blog Title

You can dinamically change your Blogger Blog title to whatever you want (in this case you're changin it into 'NEW KICK-ASS TITLE') using the following Javascript snippet:


var myKickAssScript = "document.getElementById('header-inner').getElementsByTagName('h1')[0].innerHTML = 'NEW KICK-ASS TITLE'";
setTimeout(myKickAssScript, 2000);



We have to set a timeout in order to wait for the title element to be injected by the blogger engine. You can put this js code in the script element that's in the head section of the markup (you can edit it from 'edit HTML' section on your blog settings). 
If you put the snippet (passed as string to the timeOut function above) at the bottom of the page (or as event handler for the onLoad event) you obviously don't need to set up a timer.


It'll work as long as they decide to keep the page markup as it is now.

P.S. check-out my NEW KICK-ASS TITLE

Wednesday, March 19, 2008

[CSS, Javascript] How to make your <DIV> disappear


This is a quick how to for beginners.
I'll show you how to make a DIV element disapper using a button, like in this way:

This div will disapper!!!

This is done changing the class attribute of the selected DIV, as is done in the following javascript function.

<script type="text/javascript">

function hideDiv()

{

var isIE = (window.ActiveXObject)?true:false;

var attributeClass = (isIE)?"className":"class";

var element = document.getElementById("hiddenDiv");

if(element==null)

return;

if(element.getAttribute(attributeClass)=="hidden")

element.setAttribute(attributeClass, "visible");

else

element.setAttribute(attributeClass, "hidden");

}

</script>


This won't work without the definitions of the CSS classes used and the button that send the event onClick:

<STYLE>

.hidden{ display:none; }

.visible{}

</STYLE>



<input type="button" onClick="javascript:hideDiv();" value="hide"/>



<div id="hiddenDiv" >This div will disapper!!!</div>


Actually the .visible class is empty, because by default an element will be visible, so there is no need to specify that it should be visible: this is needed just to override the .hidden class.
You can use the hideDiv method with each control you wish to use that can launch an event (an hyperlink, an image, ...).

Besides, you can use also another CSS attribute, the visibility attribute; this simply makes the entity invisible/visibile, but it still occupies its space: that's why you need to insert manually a zero width and height, like in this way:
.hidden{ visibility:none; width:0px; height:0px;}
.visible{visibility:visible; }


And that's all!
See ya!

Sunday, January 27, 2008

[Javascript, DHTML] Easiest way to add del.icio.us post from Blogger

problem: There's no way in hell you can find any ready-to-use pluggable html snippet to add the current page of your blogger blog (current version) to the user's del.icio.us links.

solution: copy-paste the following wherever you want:

<a href='' id='delicious'>
<img alt='Add the butchers to your Del.icio.us!'
height='15'
src='http://images.del.icio.us/static/img/delicious.42px.gif'/>

</a>
<script language='javascript'
type='text/javascript'>

var currentURL = window.location;
document.getElementById('delicious').href =
"http://del.icio.us/post?url=" + currentURL;

</script>


I am lazy, so I was looking for something to copy-paste into the html and amen. After an hour or so of roaming without finding anything usable as-it-is, I decided my laziness was costing me more than actually making the thing myself (what the heck). So I did it. This technique obviously will work with any of the linksharing networks (digg and others) or whatever, but the difference is I could find ready-to-plug snippet for the others. Maybe it'll save some precious hours of laziness to some other copy-paste butcher.