DEV Community

bhagvan kommadi
bhagvan kommadi

Posted on

Javascript tips

Below are some of the tips in javascript and html.

html select
<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>
</select>

String functions
key = chart_id.split("_");
chart_name = key[1];
if(char_name.includes("val_"))
{
}

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

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

div updates and removal
$('#myCharr').update()
$('#myCharr').remove();

lambda function
keys = key.map((x) => x);

Handling Data tables and wildcard query selector
$(document).ready(function() {
$('#example').dataTable();
} );
$("table[id^='Table_']")

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) {
});
document.getElementById('dataframess').value;

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];
if(y=="")
{
y="100";
}
else
{
y= String(y);
y = y.substring(0, y.length - 1);;
}
////alert("y"+y);
test_with_index.push([parseInt(y), i]);
}

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

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 = event.target.id;
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)

Collapse
tzwel profile image
tzwel

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

Collapse
callmebobonwa profile image
Nnaji David

Can you try next time to break it down for us