Group Study at Sharptop 

Session 2

Code of Conduct

Document Object Model

CSS Selectors

Selector Examples



   <div class="something"> </div>

   <div id="something"> </div>


   <div class="something" data-id="2"> </div>


   <something> </something>


<div class="something" class="col-xs-8"> </div>


    <div class="something">
        <div> Item One </div> 
        <div> Item Two </div> 

.something a

    <div class="something">
        <a href="#"> One </a>
        <a href="#"> Two </a>
            <a href="#"> Three </a>    

Child Selectors

.something > a


    <div id="something" class="else">
        <a href="#"> One </a>
        <a href="#"> Two </a>
        <div class="else">
            <a href="#"> Three </a>    

Multiple Selectors

#something  .else


or "Why is Ma CSS Not Working?"  

    <div id="something" class="else">
        <a href="#"> One </a>
        <a href="#"> Two </a>
        <div class="else">
            <a href="#"> Three </a>    

    div#something a { border: 1px solid #ccc; }
    a { border: 1px dotted #ccc; }
    div a { border: 1px dashed #ccc; }


CSS is too cool for scope, you peasants need to worry about it!

Be specific about your classes

    .box { border-color: blue; }

    /* vs. */ 

    #home .box-blue { border-color : blue;  }

Scope it yourself

    .myapp-box-blue { border-color : blue;  }

Remember specificity

Basic jQuery

"It makes things like HTML document traversal and manipulation, event handling, animation, and Ajax much simpler with an easy-to-use API that works across a multitude of browsers. "

How it works:

Find one or more elements

Do stuff with them

        /* Find all div elements that have class "something" */


        // Change the text of the element

        $('div.something').text("Hi y'allz")

Uses CSS Selectors

    // Basic selectors 
    $('#ht-focus-wrap').css('width',  '1200px');
    // You can use variables to build your selectors.     
    var module_index = 3; 
    var offset = $('.ht-tab[data-index="'+module_index+'"] > .ht-tab-content')

    // You don't have to use just selectors for finding elements

Examples for Findings Things


Find descendants everything below


Find children, immediately below


Find parents, everything above

Find parent, immediately above


Find closest, wherever is closest


Find next sibling


CSS Examples


Add a class name 


Remove a class name


Check if it has a class (returns boolean) 

Change layout


Change any kind of css

    // For one 
    $('.else').css('background-color', '#ccc');

    // For multiple, pass object    
            "background-color" : "#ccc",
            "display" : block

Manipulate HTML

    $('.something').html('<div class="newdiv"> New </div>');

Change innerHTML

    $('.newDiv').wrap('<div class="div-wrap"></div>');

Wrap html around something

    $('.something').append('<div> Last thing </div>');

Add HTML to the end of parent

Remove element (not just hide) 


Replace contents of element

    $( "<h2>New heading</h2>" ).replaceAll( ".inner" );

Clone element and do things with the clone

    $( ".hello" ).clone().appendTo( ".goodbye" );

Other Examples

    var color = $('.something').attr('data-color');

Work with attributes

    var name = $('input[type="text"]').val();

Get form input values


Show and hide elements

Getting vs Setting

    var color = $('.something').css('background-color');

    var width = $('#else').width();

    var title = $('h1.title').text(); 

Getting the existing value

Setting new value

    $('.something').css('background-color', '#eee');


    $('h1.title').text('This is my Title'); 

Debugging Tip

Are you selecting the right things? 

    // Log it
    var selector = $('h1.title')
    console.log("Title selector', selector); 

    // Get the size 
    var matches = $('h1.title').length; 

jQuery does not throw error when it can't find the elements you are looking for. 

Check in your browser to avoid console logging everything, dev tools will give errors.

Is the thing you are selecting there when it's called

Building a Portfolio

Your own portfolio website

Your github account

Live examples of your code


100 Things Every Designer Needs to Know About People

by Susan Weinschenk

Editor: Brackets*

Free, open source and has live edit. 

*great for learning and stand alone builds but too limited for production

Resources to learn

Learn jQuery

jQuery Documentation

Using jQuery

By Caner Uguz

Using jQuery

Introduction to jQuery

  • 321
Loading comments...

More from Caner Uguz