Click here to Skip to main content
15,895,746 members
Home / Discussions / JavaScript
   

JavaScript

 
AnswerRe: how to open word file in client side Pin
Richard MacCutchan26-Jun-19 1:45
mveRichard MacCutchan26-Jun-19 1:45 
Question(SOLVED) How to activate button on mouse press? Pin
samflex6-Jun-19 4:39
samflex6-Jun-19 4:39 
AnswerRe: How to activate button on mouse press? Pin
Richard Deeming6-Jun-19 5:39
mveRichard Deeming6-Jun-19 5:39 
GeneralRe: How to activate button on mouse press? Pin
samflex6-Jun-19 6:24
samflex6-Jun-19 6:24 
GeneralRe: How to activate button on mouse press? Pin
samflex6-Jun-19 7:21
samflex6-Jun-19 7:21 
GeneralRe: How to activate button on mouse press? Pin
Richard Deeming6-Jun-19 8:03
mveRichard Deeming6-Jun-19 8:03 
GeneralRe: How to activate button on mouse press? Pin
samflex6-Jun-19 9:47
samflex6-Jun-19 9:47 
Questionhow to count product codes from textarea? Pin
Veltix26-May-19 4:11
Veltix26-May-19 4: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 11:14
professionaljkirkerx27-May-19 11:14 
QuestionBest ReactJS tutorial? Pin
Corey Cananza26-May-19 3:59
Corey Cananza26-May-19 3:59 
AnswerRe: Best ReactJS tutorial? Pin
Katie Webber30-Jul-19 2:06
professionalKatie Webber30-Jul-19 2:06 
AnswerRe: Best ReactJS tutorial? Pin
Harsh.Shah.ifour13-Aug-19 18:13
professionalHarsh.Shah.ifour13-Aug-19 18:13 
AnswerRe: Best ReactJS tutorial? Pin
Le centriste4-Sep-19 3:57
Le centriste4-Sep-19 3:57 
QuestionHow to call function second time? Pin
Tarun Rathore 23-May-19 19:28
Tarun Rathore 23-May-19 19:28 
GeneralRe: How to call function second time? Pin
Richard MacCutchan23-May-19 21:56
mveRichard MacCutchan23-May-19 21:56 
AnswerRe: How to call function second time? Pin
Shraddha_Patel21-Oct-19 20:04
Shraddha_Patel21-Oct-19 20:04 
QuestionExplain the working of timers in JavaScript? Also elucidate the drawbacks of using the timer, if any? Pin
Shraddha_Patel20-May-19 17:38
Shraddha_Patel20-May-19 17:38 
AnswerRe: Explain the working of timers in JavaScript? Also elucidate the drawbacks of using the timer, if any? Pin
Richard MacCutchan20-May-19 21:03
mveRichard MacCutchan20-May-19 21:03 
AnswerRe: Explain the working of timers in JavaScript? Also elucidate the drawbacks of using the timer, if any? Pin
ZurdoDev21-May-19 2:03
professionalZurdoDev21-May-19 2:03 
QuestionHow to give unique ids to changing divs? Pin
Tarun Rathore 18-May-19 9:48
Tarun Rathore 18-May-19 9:48 
AnswerRe: How to give unique ids to changing divs? Pin
ZurdoDev21-May-19 2:04
professionalZurdoDev21-May-19 2:04 
QuestionUsing XML Pin
V.28-Apr-19 20:57
professionalV.28-Apr-19 20:57 
SuggestionRe: Using XML Pin
Richard MacCutchan28-Apr-19 21:56
mveRichard MacCutchan28-Apr-19 21:56 
GeneralRe: Using XML Pin
V.28-Apr-19 22:21
professionalV.28-Apr-19 22:21 
GeneralRe: Using XML Pin
Richard MacCutchan28-Apr-19 22:25
mveRichard MacCutchan28-Apr-19 22:25 

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.