How to add a style to first element in css?

Question

There is a html code as below.

1
2
3
4
5
6
<div class="ctr-1">
<h3>Title</h3>
<div class="ctr-2">
<h3>Title</h3>
</div>
</div>

And I wanna apply a style to first element <h3> with .ctr-1 h3:first-child{ display:none; }, however, it will apply to all <h3>, I only want the first <h3> has this style.

Answer

:first-of-type and :nth-of-type() might be helpful here.

:first-of-type

The :first-of-type CSS represents the first element of its type among a group of sibling elements. See example below.

1
2
3
<h2>Heading</h2>
<p>Paragraph 1</p>
<p>Paragraph 2</p>
1
2
3
p:first-of-type {
color: red;
}

In this way, Paragraph 1 will be red.

:nth-of-type()

The :nth-of-type() CSS matches one or more elements of a given type, based on their position among a group of siblings.

1
2
3
4
5
6
7
8
<div>
<div>This element isn't counted.</div>
<p>1st paragraph.</p>
<p>2nd paragraph.</p>
<div>This element isn't counted.</div>
<p>3rd paragraph.</p>
<p>4th paragraph.</p>
</div>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/* Odd paragraphs */
p:nth-of-type(2n + 1) {
color: red;
}

/* Even paragraphs */
p:nth-of-type(2n) {
color: blue;
}

/* First paragraph */
p:nth-of-type(1) {
font-weight: bold;
}

In this way, 1st paragraph. and 3rd paragraph. will be red, also 2nd paragraph. and 4th paragraph. will be blue.

Besides that, 1st paragraph. will be bold as well.

Reference


This is the end of post

How to make case insensetive in Javascript?

Question

There is an if statement as below.

1
2
3
if (str.indexOf("Abc") == -1) {
console.log("pass");
}

I want to make Abc case insensitive, so Abc, abc, ABc etc will still pass through.

Answer

Add .toLowerCase() after str. This method will make all letters to lower case.

1
2
3
if (str.toLowerCase().indexOf("Abc") == -1) {
console.log("pass");
}

In this way, Abc will convert into abc.

Reference


This is the end of post

How to add an element into an array from differernt orders in Javascript?

Question

The is an array, and I wanna add an element at the beginning and the end of the array.

Answer

So the array is

1
var array = [2, 3, 4];

The new element is var newBeginning = 1;, and var newEnd = 5;.

To the beginning

If we wanna at to the beginning, we can use unshift function.

1
array.unshift(newBeginning);   // [1, 2, 3, 4]

To the end

If we wanna at to the end, we can use push function.

1
array.unshift(newEnd);   // [2, 3, 4, 5]

Reference


This is the end of post

How to wait few seconds to execute the next line in Javascript?

Question

Like title says, how to wait, let’s say 5 seconds, to execute the next line in javascript?

Answer

First, create a wait function to freeze the code:

1
2
3
4
5
6
7
function wait(ms){
var start = new Date().getTime();
var end = start;
while(end < start + ms) {
end = new Date().getTime();
}
}

Insert wait function into your code, the paramemter is millisecond.

1
2
3
console.log('before');
wait(5000);
console.log('after');

Reference


This is the end of post

How to extract value of a property as an array from an array of objects in Javascript?

Question

There is an object array:

1
objArray = [ { foo: 1, bar: 2}, { foo: 3, bar: 4}, { foo: 5, bar: 6} ];

How to extract the values of key foo into an arrya like [1, 3, 5]?

Answer

ES 6

By using map and => function can easily make it happen.

1
var result = objArray.map(a => a.foo);

ES 5

Since ES 5 doesn’t support =>, so we need a function to get the value.

1
var result = objArray.map(function(a) {return a.foo;});

Reference


This is the end of post

How to copy an object from console log in Chrome Webkit Inspector?

Question

There is a console.log from Chrome webkit inspector. How to copy it directly?

Answer

  1. Right-click an object in Chrome’s console and select Store as Global Variable from the context menu. It will return something like temp1 as the variable name.
  2. Chrome also has a copy() method, so copy(temp1) in the console should copy that object to your clipboard.

01

Reference


This is the end of post

How to replace multiple strings with multiple other strings in Javascript?

Question

I have a string called I have cats, dogs, and goats., and I want to replace to I have AA, BB, and CC..

How to make it happen only with one function?

Answer

If you only wanna it happen easily, just need one function to replace all of them.

1
2
3
4
5
6
7
8
9
var str = "I have cats, dogs, and goats";
var mapObj = {
cats:"AA",
dogs:"BB",
goats:"CC"
};
str = str.replace(/cat|dog|goat/gi, function(matched){
return mapObj[matched];
});

Reference


This is the end of post

How to get properties values of a object without knowing their keys in Javascript?

Question

So there is a javascript object like

1
2
3
4
5
var objects = {
type:"Fiat",
model:"500",
color:"white"
}

If the key names are random, and we don’t know. How to get each property’s value with a loop?

Answer

By using for..in.. loop:

1
2
3
for(var key in objects) {
var value = objects[key];
}

Reference


This is the end of post