Count number of lines displayed (not newlines) in an HTML textarea without jQuery?
Date : March 29 2020, 07:55 AM
hope this fix your issue you could try out this. i didn't test it so far, but i remember this should work fine. var lineHeight = document.getElementById("yourTextarea").style.lineHeight;
var scrollHeight = document.getElementById("yourTextarea").scrollHeight;
document.getElementById("yourTextarea").style.height = scrollHeight; // this is just for showing purposes
var numLines = Math.floor( scrollHeight / lineHeight );
<textarea id="mytext" cols="10" rows="12">
</textarea>
<input type="button" value="Count Rows" onclick="countRows();"/>
<script type="text/javascript">
function countRows() {
var stringLength = document.getElementById("mytext").value.length;
var count = Math.ceil( stringLength / document.getElementById("mytext").cols );
// just devide the absolute string length by the amount of horizontal place and ceil it
alert( count );
}
</script>
|
Count number of elements in the html table
Date : March 29 2020, 07:55 AM
help you fix your problem This javascript function will help you to retrive content of each cell and after getting the content you can write your own logic on that <script>
function GetCellValues() {
var refTab = document.getElementById("YOUR_TABLE_ID")
var ttl;
// Loop through all rows and columns of the table and popup alert with the value
// /content of each cell.
for ( var i = 0; row = refTab.rows[i]; i++ ) {
row = refTab.rows[i];
for ( var j = 0; col = row.cells[j]; j++ ) {
alert(col.firstChild.nodeValue);
}
}
}
</script>
<script>
function getAandBCount() {
var refTab = document.getElementById("YOUR_TABLE_ID")
var ttl;
var a=0;
var b=0;
for ( var i = 0; row = refTab.rows[i]; i++ ) {
row = refTab.rows[i];
for ( var j = 0; col = row.cells[j]; j++ ) {
if(col.firstChild.nodeValue == 'A'){
a++;
}else if(col.firstChild.nodeValue == 'B'){
b++;
}
}
}
alert(a);
alert(b);
}
</script>
|
Count number of elements in each row in 2D list
Tag : python , By : Picoman Games
Date : March 29 2020, 07:55 AM
I hope this helps . I have a 2D list (can be variable size depending on file) like this: partition2d = [['A', '1', '5'],
['B', '2', '3', '4'],
['C', '6', '7', '8', '9']]
guest_list = [0.5, 0.0, 1.0]
numbers = [len(part)-1 for part in partition2d]
numbers
#>>> [2, 3, 4]
numbers = [sum(v.isnumeric() for v in part) for part in partition2d]
numbers
#>>> [2, 3, 4]
total_numbers = sum(numbers)
total_numbers
#>>> 9
[n/total_numbers * factor for n, factor in zip(numbers, guest_list)]
#>>> [0.1111111111111111, 0.0, 0.4444444444444444]
|
Count number of specific elements in between other elements in list
Date : March 29 2020, 07:55 AM
I wish did fix the issue. The following code should work if your datafile looks exactly as you described (e.g. no other number except incresing number of step and zeroes). cur = 0
res = []
with open("file.txt") as f:
for line in f:
if line.strip() == '0':
cur += 1
else:
res.append(cur)
cur = 0
|
How to count the number of checked elements and the number of total elements in a to-do-list
Date : March 29 2020, 07:55 AM
With these it helps Remove the event handler inside the taskAmount() function and call the function from add tasks fixes the issue. Also made your id unique for the generated content. tasks = [];
var idCount = 1;
function addTask() {
var li = document.createElement("li");
// Adding checkbox
var cbox = document.createElement('INPUT');
cbox.setAttribute('type', 'checkbox');
cbox.addEventListener('change', taskAmount);
li.appendChild(cbox);
cbox.setAttribute('id', 'box' + idCount);
idCount++;
// Adding thing to do
var inputValue = document.getElementById("newTask").value;
var task = document.createTextNode(inputValue);
li.appendChild(task);
if (inputValue === '') {
alert("You must write something!");
} else {
var ullist = document.getElementById("list");
ullist.insertBefore(li, ullist.childNodes[0]);
}
// Part 3
tasks.push(newTask.value);
document.getElementById("newTask").value = '';
taskAmount();
}
// Prevent Default
document.getElementById("submit").addEventListener("click", function() {
event.preventDefault()
});
// Part 4
function ifChecked() {
var list1 = document.querySelector('ul');
list1.addEventListener('click', function(ev) {
if (ev.target.tagName === 'INPUT') {
ev.target.parentElement.classList.toggle('checked');
}
}, false);
}
ifChecked();
// Part 5
function taskAmount() {
var totalTasks = tasks.length;
var checkedTasks = document.querySelectorAll('input[type="checkbox"]:checked').length;
console.log(totalTasks);
console.log(checkedTasks);
}
body {
width: 600px;
margin: auto;
background: #ffe699;
font-family: garamond;
font-size: 25px;
}
ul {
list-style-type: none;
padding: 0;
border: 2px solid black;
}
ul li {
padding-top: 15px;
padding-bottom: 15px;
font-weight: bold;
transition: 0.3s;
}
fieldset {
background: skyblue;
border: 2px solid black;
}
ul li:nth-child(odd) {
background-color: skyblue;
}
ul li:nth-child(even) {
background-color: white;
}
ul li:hover {
background-color: #55D;
}
#box {
margin: 0px 20px 0px;
}
.checked {
text-decoration: line-through;
}
HTML
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title> To do list </title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1> To do list </h1>
<form class="" action="todo.html" method="post">
<fieldset>
<label> Add a task in the input field below </label> <br><br>
<input id="newTask" type="text" placeholder="Type a task here" autofocus> <br><br>
<input id="submit" onclick="addTask()" type="submit" value="Add task"><br><br>
<output name="result"> </output>
</fieldset>
</form>
<h2>List of things to do</h2>
<ul id="list">
</ul>
<script src="todo.js">
</script>
</body>
</html>
|