Click here to Skip to main content
14,391,232 members

JavaScript

 
AnswerRe: how to open word file in client side Pin
Richard MacCutchan26-Jun-19 2:45
mveRichard MacCutchan26-Jun-19 2:45 
Question(SOLVED) How to activate button on mouse press? Pin
samflex6-Jun-19 5:39
Membersamflex6-Jun-19 5:39 
AnswerRe: How to activate button on mouse press? Pin
Richard Deeming6-Jun-19 6:39
communityengineerRichard Deeming6-Jun-19 6:39 
GeneralRe: How to activate button on mouse press? Pin
samflex6-Jun-19 7:24
Membersamflex6-Jun-19 7:24 
GeneralRe: How to activate button on mouse press? Pin
samflex6-Jun-19 8:21
Membersamflex6-Jun-19 8:21 
GeneralRe: How to activate button on mouse press? Pin
Richard Deeming6-Jun-19 9:03
communityengineerRichard Deeming6-Jun-19 9:03 
GeneralRe: How to activate button on mouse press? Pin
samflex6-Jun-19 10:47
Membersamflex6-Jun-19 10:47 
Questionhow to count product codes from textarea? Pin
Veltix26-May-19 5:11
MemberVeltix26-May-19 5:11 
I want count in my textarea product codes.
I have whitelist for product codes so I cant type or write anything else into textarea.
Found bad issue, I can write only static product codes like (first 16 is product code other 6 is how many products it is in list...):

0FAR12345H00123C + 00001P
0FAR54321H00321C + 00001P
0FAR54321H00321C + 00002P

So now I have two problems..
First problem, whitelist universal product codes like this:

0FAR12345H00123C + 00001P
0FAR543H00321C + 00001P
0F321H321C + 00001P
0R31H1C + 001P

Second problem is how to count these product codes under textarea + overall count.

Example under textarea counts:
0FAR12345H00123C: 5
0FAR543H00321C: 2
0F321H321C: 6
0R31H1C: 4
Overall: 17

Javascript:

<pre>
"use strict";
	toFile.focus();

	var timeoutId;
	$('#toFile').on('keypress', function() {
	    console.log('Textarea Change');
	    
	    clearTimeout(timeoutId);
	    timeoutId = setTimeout(function() {
	        // Runs 3 second (3000 ms) after the last change    
	        saveToFile();
	    }, 3000);
	});

	toFile.addEventListener("keyup", event => {
	    const data = toFile.value.split("\n");
	    const result = data.unique();
	    info.textContent = result.length !== data.length ? "Duplicate removed" : "";
	    toFile.value = result.join('\n');
	});

	if (!Array.prototype.unique) {
	    Object.defineProperty(Array.prototype, "unique", {
	        configurable: false,  // these 3 properties default to false so not needed
	        enumerable: false,    // But have added them just to show their availability.
	        writable: false,         
	        value: function() { 
	            const existing = {};
	            return this.filter(a => existing[a] = !existing[a] ? true : false);
	        }
	    });
	} else {
	    throw new Error("Array.prototype.unique already defined.");
	}

	const CODE = ['Product', 'Codes', 'Here'];
	const reg = /^([a-zA-Z0-9]{1,6})$/;

	// Ensure values been inserted are in correct format
	function onInput() {
	  let values = event.target.value
	    .split('\n')
	    .map(v => {
	      v = v.trim().toUpperCase();
	      if (v.length <= 16) {
	        if (CODE[0].substr(0, v.length) != v &&
	          CODE[1].substr(0, v.length) != v) {
	          v = v.substr(0, v.length - 1);
	        }
	      } else if (!v.substr(16, 22).match(reg)) { // at the moment static 16 product code lenght + 6 is count of products.
	        v = v.substr(0, v.length - 1);
	      }
	      return v;
	    }).join('\n');

	  event.target.value = values;
	}
	
	function saveToFile() {
	    console.log('Saving to the db');
	    toFile = $('#toFile').val().replace(/\n\r?/g, '<br />');
		$.ajax({
			url: "test2.php",
			type: "POST",
			data: {toFile:toFile}, // serializes the form's elements.
			beforeSend: function(xhr) {
	            // Let them know we are saving
				$('#status').html('Saving...');
			},
			success: function(toFile) {
				// You can get data returned from your ajax call here. ex. jqObj.find('.returned-data').html()
	            // Now show them we saved and when we did
	            var d = new Date();
	            $('#status').html('Saved! Last: ' + d.toLocaleTimeString());
			},
		});
	}

	$('.form').submit(function(e) {
		saveToFile();
		e.preventDefault();
	})


HTML:

<form class="contact-form" method="post">
    <textarea id="toFile" name="toFile" oninput="onInput()"></textarea><br>
    <input type="submit" value="Submit">
</form>

<!-- under here I want show count of product codes + overall count -->

<div id="status"></div>
<div id="info"></div


This is how I want count product codes under textarea + overall count:
Image

This shows what you can only type in textarea (Whitelisted product codes):
Image
AnswerRe: how to count product codes from textarea? Pin
jkirkerx27-May-19 12:14
professionaljkirkerx27-May-19 12:14 
AnswerMessage Closed Pin
7-Jun-19 0:13
Memberananyagupta7-Jun-19 0:13 
QuestionBest ReactJS tutorial? Pin
Corey Cananza26-May-19 4:59
MemberCorey Cananza26-May-19 4:59 
AnswerMessage Closed Pin
13-Jun-19 21:18
Membersasi Tamil13-Jun-19 21:18 
AnswerRe: Best ReactJS tutorial? Pin
Katie Webber30-Jul-19 3:06
professionalKatie Webber30-Jul-19 3:06 
AnswerRe: Best ReactJS tutorial? Pin
Harsh.Shah.ifour13-Aug-19 19:13
professionalHarsh.Shah.ifour13-Aug-19 19:13 
AnswerMessage Closed Pin
19-Aug-19 21:40
MemberMember 1456332319-Aug-19 21:40 
AnswerMessage Closed Pin
19-Aug-19 21:46
MemberMember 1456332319-Aug-19 21:46 
AnswerRe: Best ReactJS tutorial? Pin
Le centriste4-Sep-19 4:57
MemberLe centriste4-Sep-19 4:57 
QuestionHow to call function second time? Pin
Tarun Rathore 23-May-19 20:28
MemberTarun Rathore 23-May-19 20:28 
GeneralRe: How to call function second time? Pin
Richard MacCutchan23-May-19 22:56
mveRichard MacCutchan23-May-19 22:56 
AnswerRe: How to call function second time? Pin
Shraddha_Patel21-Oct-19 21:04
MemberShraddha_Patel21-Oct-19 21:04 
QuestionExplain the working of timers in JavaScript? Also elucidate the drawbacks of using the timer, if any? Pin
Shraddha_Patel20-May-19 18:38
MemberShraddha_Patel20-May-19 18:38 
AnswerRe: Explain the working of timers in JavaScript? Also elucidate the drawbacks of using the timer, if any? Pin
Richard MacCutchan20-May-19 22:03
mveRichard MacCutchan20-May-19 22:03 
AnswerRe: Explain the working of timers in JavaScript? Also elucidate the drawbacks of using the timer, if any? Pin
ZurdoDev21-May-19 3:03
professionalZurdoDev21-May-19 3:03 
QuestionHow to give unique ids to changing divs? Pin
Tarun Rathore 18-May-19 10:48
MemberTarun Rathore 18-May-19 10:48 
AnswerRe: How to give unique ids to changing divs? Pin
ZurdoDev21-May-19 3:04
professionalZurdoDev21-May-19 3:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.