Friday, April 27, 2012

Disable Enable BUTTON using query

Disable Enable BUTTON using query :

Disable :
      Using Id :
             $('#ButtonId').attr('disabled', 'disabled');
      Using Class:
              $('.ButtonClass').attr('disabled', 'disabled');

Enable :
     Using Id :
              $('#ButtonId').removeAttr('disabled');
     Using Class:
              $('.ButtonClass').removeAttr('disabled');


Cheers,

Check defined variable exists or not in Javascript

How to check defined variable exists or not in Javascript :

There are few ways to check if any variable exists or not

1) if(typeof IdOfTheVariable != 'undefined')
     {
             //Do Something
      }

or

2) if(window.IdOfTheVariable === undefined)
    {
        //Do Something
    }


Cheers,

Monday, April 16, 2012

Break a continues sentence into words using the capital letters in xslt ...

Sometimes we need to break a chunk of sentence which are without spaces into the words like

Input : ManojJoshi
OutPut: Manoj Joshi

In XSLT

First of all we need to create a template which will call recursively and return words... so here is the template :


<!--template to break the String into words -->
<xsl:template name="Split">
<xsl:param name="Value"/>
<xsl:param name="First" select="false()"/>
<xsl:if test="$Value!=''">
<xsl:variable name="Space">*</xsl:variable>
<xsl:variable name="FirstChar" select="substring($Value, 1, 1)"/>
<xsl:variable name="Rest" select="substring-after($Value, $FirstChar)"/>
<xsl:if test="not($First)">
<xsl:if test="translate($FirstChar, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', '..........................')= '.'">
<xsl:value-of select="$Space"/>
</xsl:if>
</xsl:if>
<xsl:value-of select="$FirstChar"/>
<xsl:call-template name="Split">
<xsl:with-param name="Value" select="$Rest"/>
</xsl:call-template>
</xsl:if>
</xsl:template>

So that's all ... we need to call the template like below :
<xsl:call-template name="Split">
<xsl:with-param name="Value" select="'ManojJoshi'"/>
<xsl:with-param name="First" select="true()"/>
</xsl:call-template>

And we are done here ..

Insert default option to the drop downs using jQuery

There are some cases where we need to add dynamically a default value to the dropdowns. A simple code snippet which enters default option to all the dropdown inside a container :

//Used to insert default value for the dropdowns
function InsertFirstOptionToDropdowns(divId) {

//Find all the select and add span
$("#divContainer").find('select').each(function () {
$(this).prepend("<option value='' selected='selected'>Please Select</option>");
});

}

cheers

Wednesday, March 28, 2012

Fix days of month depending on particular year for dropdowns

Fix days of month depending on particular year:
There are some cases where we have two dropdowns(for month and days) and upon selecting month days dropdown needs to be update so i wrote a few lins of code for it and hope it helps anyone face this situation

This is the method which actually returns the number of days available for a month and year:

correctDaysInMonth = 32 - new Date(year, month, 32).getDate();

function which adjust the nmber of days dropdown upon selecting year dropdown :

function AdjustDaysOfTheMonth(controlID, month, year) {

//Calculate exact number of day's in a month
//when we pass the parameter days greater than the particular month available days
// It throws the actual number of days available

correctDaysInMonth = 32 - new Date(year, month, 32).getDate();

console.log('exact Dates : ' + controlID + ' : ' + +correctDaysInMonth);

var maxDayValue = Number($(controlID + " option:last-child").val());

if (maxDayValue != correctDaysInMonth) {
// If maximum value of the days dropdown is less than the
// actual days add additional days
if (maxDayValue < correctDaysInMonth) {

//Add element from the range of month
for (var count = maxDayValue + 1; count <= correctDaysInMonth; count++) {

//Create option element
var dayElementOption = document.createElement('option');

dayElementOption.text = count;

dayElementOption.value = count;

//Add it to control
try {
$(controlID)[0].add(dayElementOption, null); // Fail in IE
}
catch (ex) {
$(controlID)[0].add(dayElementOption); //work in IE only
}
}
}
else {
//Remove element not in the range of month
for (var count = maxDayValue + 1; count > correctDaysInMonth; count--) {

$(controlID)[0].remove(count);

}
}
}
}