DEV Community

bhagvan kommadi
bhagvan kommadi

Posted on

Javascript tips

Below are some of the tips in javascript and html.

html select
<option value="" selected disabled hidden>Choose here</option>
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
<option value="4">Four</option>
<option value="5">Five</option>

String functions
key = chart_id.split("_");
chart_name = key[1];

For loop - alternate elements (even and odd)
for(var i=0; i< arr.length; i++)
if(i%2 == 0)
else if(i%2 != 0)

Slice function
coars.slice(0,val_count); // not inclusive of the end

div updates and removal

lambda function
keys = => x);

Handling Data tables and wildcard query selector
$(document).ready(function() {
} );

Object.keys iterating through dictionary keys and values
for (var i = 0; i < Object.keys(params).length; i++) {
count_params[Object.keys(params)[i]] = 0;

Query selector for event listener and accessing value

$('button[id^="charts_"]').on('click', function (event) {

Sort the array with the index to find out the sorted order
function sort_with_index(test)
var test_with_index = [];
for (var i in test) {
var y = test[i];
y= String(y);
y = y.substring(0, y.length - 1);;
test_with_index.push([parseInt(y), i]);

dynamic url href and clicking on it
var a = document.createElement('a'); = 'roles_org.png';
a.href = base64_url;;

highlighting the text in a table
$("#display_table").mousedown(function (event) {
mouseXPosition = event.pageX; //register the mouse down position
$("#display_table").mouseup(function (event)
target =;
var highlighted = false;
var selection = window.getSelection();
var selectedText = selection.toString(); alert("selectedText"+selectedText);

mark for highlighting

<mark style='color: white; background-color: "+colored+";'>" + selectedTexts + "</mark>

Discussion (2)

tzwel profile image

holy fucking shit, i could write an essay on how bad those code practices are

also the article is unreadable, highlight your syntax, format it properly, and state that jquery is used in your examples

callmebobonwa profile image
Nnaji David

Can you try next time to break it down for us