Archive for tag: class

Shorten up this overly long JavaScript-jQuery function

Recently, I have been aseked this question and found bit of good solution as below:

I have a simple script that selects a class's and adds a class of selected to it and remove it from all other 's with the similar class. It all works perfectly, but I know that there has to be a better way to write this out, I just can't figure it out.. Does it involve something like this?

$('.adv-option-set a').click(function()
{
    // if clicked item is selected then do nothing
    if ($(this).hasClass('selected')){}

    // otherwise deselect all and select just this one
    else
    {
        $('.adv-option-set a').removeClass('selected');
        $(this).addClass('selected');
    }
});

$('.adv-option-set2 a').click(function()
{
    // if clicked item is selected then do nothing
    if ($(this).hasClass('selected')){}

    // otherwise deselect all and select just this one
    else
    {
        $('.adv-option-set2 a').removeClass('selected');
        $(this).addClass('selected');
    }
});

And below is HTML that has been provided

     <article id="filter">
        <ul id="filter-nav" class="option-set">
          <li>Filter: </li>
          <li><a data-categories="*" data-subid="all" class="selected">All</a></li>
          <li><a data-categories="tubs-and-showers" data-subid="tubs-and-showers">Tubs &amp; Showers</a></li>
          <li><a data-categories="countertops" data-subid="countertops">Countertops</a></li>
          <li><a data-categories="faucets" data-subid="faucets"><s>Faucets</s></a></li>
          <li><a data-categories="cabinetry" data-subid="cabinetry"><s>Cabinetry</s></a></li>
          <li><a data-categories="flooring" data-subid="flooring"><s>Flooring</s></a></li>
          <li><a data-categories="toilets" data-subid="toilets"><s>Toilets</s></a></li>
          <li><a data-categories="accessories" data-subid="accessories"><s>Accessories</s></a></li>
        </ul>
        <div id="advfilter" class="advfilter filter-nav hidden">
          <ul id="tubs-and-showers" class="adv-option-set1">
            <li><span class="small">ADV</span> Filter: </li>
            <li><a data-categories="modular">Modular</a></li>
            <li><a data-categories="custom">Custom</a></li>
          </ul>
          <ul id="countertops" class="adv-option-set2">
            <li><span class="small">ADV</span> Filter: </li>
            <li><a data-categories="marble">Marble</a></li>
            <li><a data-categories="solid-surface">Solid Surface</a></li>
            <li><a data-categories="laminate"><s>Laminate</s></a></li>
            <li><a data-categories="granite"><s>Granite</s></a></li>
          </ul>
          <ul id="faucets" class="adv-option-set3">
            <li><span class="small">ADV</span> Filter: </li>
            <li><a data-categories="">Coming Soon</a></li>
            <li><a data-categories="">Coming Soon</a></li>
          </ul>
          <ul id="cabinetry" class="adv-option-set4">
            <li><span class="small">ADV</span> Filter: </li>
            <li><a data-categories="">Coming Soon</a></li>
            <li><a data-categories="">Coming Soon</a></li>
          </ul>
        </div>
      </article>

Here is the solution:

$('[class^=adv-option-set] a').click(
    function()
    {
       $(this).addClass('selected');
       $('[class^=adv-option-set] a').not(this).removeClass('selected');
    }
);

It is very simple but most effective and reduce code redundancy as well.

C# interface instance-like

to work with a library I just found I need to implement a few interfaces first. But some methods seem to be asking for objects that have the type of some interfaces... And if I have an interface called MyInterface I can write things like :

    MyInterface shoe;

It does not really make sense to me. Can somebody teach me this concept ?

Then i have found out very nice solution about C# Inerface, Interfaces establish a contract between a class and the code that calls it. They also allow you to have similar classes that implement the same interface but do different actions or events and not have to know which you are actually working with. This might make more sense as an example so let me use same example as per your link with bit of modification:

using System; 
 
interface IPerl 
{ 
    void Read(); 
} 
 
class Test : IPerl 
{ 
    public void Read() 
    { 
    Console.WriteLine("Read Test"); 
    } 
} 
 
class Test1 : IPerl 
{ 
    public void Read() 
    { 
    Console.WriteLine("Read Test1"); 
    } 
} 
 
class Program 
{ 
    static void Main() 
    { 
    IPerl perl = new Test(); // Create instance of Test. 
    perl.Read(); // Call method on interface output will be different then Test1. 
 
        perl = new Test1(); // Create instance of Test1. 
    perl.Read(); // Call method on interface output will be different then Test. 
 
    } 
} 

Output:

  1. "Read Test"
  2. "Read Test1"

I hope this would help.

URL fragments or parts and get Paths using HttpRequest in C#

URL fragments or parts and get Paths using HttpRequest, If we want URL fragments or parts of the URL we can get that in C# using HttpRequest as like follwoing:

In following example we have used those properties of .Net Framework Library

Below is just example URL that has taken for us to understand:

http://www.example.com:80/Products/Product-name/Detail.aspx/path/info?q=item#classic

as below

HttpRequest path and URL fragments / parts

Request.ApplicationPath: /Products

Request.CurrentExecutionFilePath: /Products/Product-name/Detail.aspx

Request.FilePath: /Products/Product-name/Detail.aspx

Request.Path: /Products/Product-name/Detail.aspx/path/info

Request.PathInfo: /path/info

Request.PhysicalApplicationPath: D:\Inetpub\wwwroot\CambiaWeb\Products\

Request.RawUrl: /Products/Product-name/Detail.aspx/path/info?q=item

Request.Url.AbsolutePath: /Products/Product-name/Detail.aspx/path/info

Request.Url.AbsoluteUri: http://www.example.com:80/Products/Product-name/Detail.aspx/path/info?q=item

Request.Url.Classic:

Request.Url.Host: www.example.com

Request.Url.LocalPath: /Products/Product-name/Detail.aspx/path/info

Request.Url.PathAndQuery: /Products/Product-name/Detail.aspx/path/info?q=item

Request.Url.Port: 80

Request.Url.Query: ?q=item

Request.Url.Scheme: http

Request.Url.Segments: /
Products/
Product-name/
Detail.aspx/
path/
info

Those are all the ways to get full, partial, fragments or parts of the URL.

THank you very much for going through this article.