Re-order element on type in input in jQuery

Reorder div or element based on input value change.

–HTML File

<!DOCTYPE html>
<html>
<head>
<title>Order Input on keypress</title>
<linkrel="stylesheet"href="css/style.css">
</head>
<body>
<divclass="container">
<h2>Re-order Input list</h2>
<article>
<aside data-id="1"><input type="text"></aside>
<aside data-id="2"><input type="text"></aside>
<aside data-id="3"><input type="text"></aside>
<aside data-id="4"><input type="text"></aside>
<aside data-id="5"><input type="text"></aside>
</article>
<h2>Re-order Input list</h2>
<article>
<aside data-id="1"><input type="text"></aside>
<aside data-id="2"><input type="text"></aside>
<aside data-id="3"><input type="text"></aside>
<aside data-id="4"><input type="text"></aside>
<aside data-id="5"><input type="text"></aside>
</article>
</div>
<script src="js/jquery-3.3.1.js"></script>
<script src="js/script.js"></script>
</body>
</html>

–CSS File

*{
    margin:0;
    padding:0;
    box-sizing: border-box;
    outline: none;
}
body{
    font-family: Arial, Helvetica, sans-serif;
    background: #f5f5f5;
}
.container{
    width: 500px;
    max-width: 90%;
    margin:50px auto;
}
h2{
    margin-bottom: 20px;
}
article{
    background: #fff;
    padding: 20px;
}
aside{
    margin:0 0 20px;
}
aside input{
    width: 100%;
    height: 40px;
    border: 1px solid #ccc;
    padding: 5px 10px;
}
aside input:focus{
    box-shadow: 0px 0px 8px #ccc;
}

–JS File

$(document).ready(function($){
    $('article').each(function(){
        $('aside').each(function(){
            $(this).find('input').val($(this).attr('data-id'));
        });
    })
    $('article aside input').change(function(){
        var inputV = $(this).val();
        if(inputV == ''){
            console.log('Please add atleast a value.');
        }
        if(isNaN(inputV)){
            console.log('Please add a number');
        }
        $(this).parent().attr('data-id', inputV);
        var wrapper = $(this).parent().parent('article');
        wrapper.find('aside').sort(function(a, b){
            return +a.dataset.id - +b.dataset.id;
        }).appendTo(wrapper);
    })
})

Use Iframe inner HTML height to iframe

–JS File

$(window).load(function(){
	SetIframe()
});
$(window).resize(function(){
	SetIframe()
});
function SetIframe(){
	var iframe = document.querySelector('.page-content iframe');
	var iframeHtml = iframe.contentDocument.children[0];
	iframe.style.height = iframeHtml.offsetHeight + 'px';
}

add and remove class in javascript

–JS File

window.onload = function(){
	var fixedForm = document.querySelector('.fixed_form');
	var fixedBtn  = document.querySelector('.fixed_form .btn_');
	fixedBtn.onclick = function() {
		if(fixedForm.classList.contains('active')){
			fixedForm.classList.remove('active');
		} else {
			fixedForm.classList.add('active');
		}
		return false;
	}
}

Please Don’t remove window.onload

Sort Array without sort function in javascript

–JS File

r =[3,4,5,1];

function isSorted(r){
	for (x=0;x<r.length-1;x++){ if (r[x] > r[x+1]){ return false;} 	
	}
	return true;
}


while (!isSorted(r)){
	for (x=0;x<r.length-1;x++){ if (r[x] > r[x+1]){
		  rx=r[x];
		  r[x]=r[x+1];
		  r[x+1]=rx;	
	  }	
	}
}


console.log(r);

Copy element in Javascript

Clone element more than one time.

–JS File

function mySvg(){
varnumberS=document.querySelector('svg.Layer_1').cloneNode(true);
varinnerPart=document.querySelector('.txt_area');
innerPart.appendChild(numberS);
}
var i = 0;
while(i < 4){
mySvg();
i++;
}

Setup Grunt

Install Grunt globally.

npm install -g grunt-cli

Create a black Gruntfile.js

Create package.json with CMD.

npm init

package.json

{
“name”: “laraevents”,
“version”: “1.0.0”,
“description”: “events site”,
“main”: “Gruntfile.js”,
“scripts”: {
“test”: “echo \”Error: no test specified\” && exit 1″
},
“author”: “”,
“license”: “ISC”,
}

Now install grunt to your local project.

npm install grunt --save-dev

Install Grunt Plugins.

https://www.npmjs.com/package/grunt-contrib-watch

npm i grunt-contrib-watch

https://www.npmjs.com/package/grunt-contrib-sass

npm i grunt-contrib-sass

https://www.npmjs.com/package/grunt-contrib-pug

npm i grunt-contrib-pug

Gruntfile.js

module.exports = function(grunt){
// Project config
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
watch: {
sass: {
files: ['dev/scss/*.sass', 'dev/*.pug'],
tasks: ['sass', 'pug']
}
},
sass: {
dist: {
files: {
'html/css/style.css' : 'dev/scss/style.sass'
}
}
},
pug: {
compile: {
options: {
data: {
debug: true
}
},
files: {
'html/index.html' : ['dev/index.pug']
}
}
}
})

grunt.loadNpmTasks('grunt-contrib-sass');
grunt.loadNpmTasks('grunt-contrib-pug');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.registerTask('default', ['sass', 'pug']);
}

force width 100% into container

section width should be force 100% from left and right to window into container.

<div class="container"><article class="text">
<h4 id="contact">Contact Us</h4>
Customers worldwide trust us with their employees’ security training. We are confident we can take some load off you.

Please use the form below to drop us a message and one of our experts will be with you soon.

</article></div>

–JS Code

var contW = $('.contact').width();
var contWA = $('.contact .text').width();
var final = contW - contWA
$('.contact .text').css('margin-left', -final/2).css('margin-right', -final/2).css('padding-right', final/2).css('padding-left', final/2)

<div class="container">
<article class="text" style="margin-left: -466.5px; margin-right: -466.5px; padding-right: 466.5px; padding-left: 466.5px;">
<h4 id="contact">Contact Us</h4>
<p>Customers worldwide trust us with their employees’ security training. We are confident we can take some load off you.</p>
<p>Please use the form below to drop us a message and one of our experts will be with you soon.</p>
</article>
</div>

Youtube video is not autoplay

We saw this type issue because youtube iframe code autoplay is not working on our web page. But if you were check it’s URL is working properly.

–jQuery

$('iframe').src = $('iframe').src

–JS

var youTube = document.querySelector('iframe');
youTube.src = youTube.src;

Chessboard in Javascript

–HTML

<div class="frame"></div>

–CSS

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.frame {
  width: 402px;
  height: 402px;
  margin: 20px auto;
  border: 1px solid #000;
}
.frame span {
  width: 50px;
  height: 50px;
  display: inline-block;
  float: left;
}
.frame span.black {
  background: #000;
}

–JS

var frame = document.querySelector('.frame');

for(var x=1; x<=8; x++){
  for(var y=1; y<=8; y++){
    if((x + y) % 2 == 0){
      frame.innerHTML += "";
    } else {
      frame.innerHTML += "";
    }
  }
}

[codepen_embed height=”265″ theme_id=”0″ slug_hash=”ZoePVq” default_tab=”css,result” user=”pradeepanvi”]See the Pen Chessboard in Javascript by Pradeep Kumar (@pradeepanvi) on CodePen.[/codepen_embed]

Find Smallest height in jQuery

Find small height in jQuery

var allDivs = jQuery('.gallery_thumb img');
var dvSmallest = allDivs[0];
$(allDivs).each(function() {
if($(this).height() < $(dvSmallest).height()){
dvSmallest = jQuery(this);
};
});
$('.gallery_thumb').css('height', dvSmallest.height());