The text box's word limiter is malfunctioning when writing code on a child page of the master

Encountering an unexpected problem that I never thought would happen. I wrote a word limiter code for a text box on a single page and it was working perfectly fine. The word limiter worked and the remaining words were displayed by a label named "remaining6". However, when I copied the same code into a child page of the master page, the word limiter functionality works but the remaining words are not displayed:

<input type="text" id="TextBox5"  placeholder="latest" runat="server"  onkeyup="countChar(this)" /> 
<h5 >Characters Left <span id="remaining6">20</span></h5>
    <script type="text/javascript" >
        function countChar(val) {
            var len = val.value.length;

            if (len >= 10) {
                val.value = val.value.substring(0, 10);                                       
            } 
           else
              {                   
                $('.numbersofChart').text(10 - len);
                alert("before");
                var textEntered = document.getElementById('TextBox5').value;
                alert("after");
                var msg = document.getElementById('remaining6');
                var counter = (10 - (textEntered.length));
                msg.textContent = counter;

            }
        };
    </script>

While debugging this code using alerts, "before" is displayed but "after" is not. This indicates an error in the line:

  var textEntered = document.getElementById('TextBox5').value;

I'm unsure why the code is not executing from this point. It's worth noting that the code runs smoothly on a single page (without the master page).The issue arises when pasted into a child page of the master. I've ensured that all necessary libraries are included in the child page. How can I solve this problem?

Answer №1

Here is an illustration of a word limit scenario:

//<![CDATA[
/* js/external.js */
var doc, bod, dE, I, wordCap; // for future use
addEventListener('load', function(){
doc = document; bod = doc.body; dE = doc.documentElement;
I = function(id){
  return doc.getElementById(id);
}
wordCap = function(context, remainElement, limit){
  var s = context.value.split(/[ ]+/), max = limit || 20;
  var n = max-s.length;
  if(n < 1)n = 0;
  remainElement.innerHTML = n;
  
  if(n === 0){
    context.value = s.splice(0, max).join(' ');
  }
}

var remainingCount = I('remain');
I('textBox').onkeyup = function(){
  wordCap(this, remainingCount);
}

}); // end load
//]]>
/* css/external.css */
*{
  box-sizing:border-box; padding:0; margin:0;
}
html,body{
  width:100%; height:100%;
}
body{
  background:#ccc;
}
#content{
  padding:7px 5px;
}
#textBox{
  width:100%; height:38px; background:#fff; color:#000; font:22px Tahoma, Geneva, sans-serif; padding:5px;
}
h5{
  text-align:center;
}
h5>span{
  color:#a00;
}
<!DOCTYPE html>
<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>
  <head>
    <meta charset='UTF-8' /><meta name='viewport' content='width=device-width, height=device-height, initial-scale:1' />
    <title>Word Limit Tracker</title>
    <link type='text/css' rel='stylesheet' href='css/external.css' />
    <script type='text/javascript' src='js/external.js'></script>
  </head>
<body>
  <div id='content'>
    <input id='textBox' type='text' />
    <h5>Words Left <span id='remain'>20</span></h5>
  </div>
</body>
</html>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Utilizing cookies to track the read status of articles - markers for reference

Currently, I am in the process of developing a website and am interested in implementing a feature that allows users to track which articles they have read. I am considering adding a small circle next to each article heading to signify whether it has been ...

Image malfunction in jquery blockui occurs following multiple AJAX requests, except when within the success function of an AJAX request

I have a perplexing question that has been on my mind. While I am aware of the issue and its resolution, I am struggling to comprehend the underlying reason. There is a particular method that goes through some preliminary steps, validation, saving, and cl ...

Angular2 Service Failing to Return Expected Value

It's frustrating that my services are not functioning properly. Despite spending the last two days scouring Stack Overflow for solutions, I haven't been able to find a solution that matches my specific issue. Here is a snippet of my Service.ts c ...

Symfony is unable to access uploaded files from an AngularJS interface

For the past 3 days, I've been grappling with uploading files via AJAX to my Symfony2 application using AngularJS. I'm utilizing my Symfony app as an API to deliver data to my Angular front end. While I can successfully post text and other data, ...

Validate input JQuery only when specific radio buttons are chosen

My form currently has basic validation set up like this: <script type="text/javascript"> $(function() { $("#postform").validate({ rules: { ...

Mono's web API seems to be stuck in a rut with 404 errors

My current project involves running an elementary Web API program on OpenSUSE using mod_mono and Apache. The code runs smoothly on my Windows machine via Visual Studio, but once I deploy it to my OpenSUSE box, I encounter nothing but 404 errors while tryi ...

Using AJAX and Jquery to stop the page from reloading when a button is clicked

I am currently working on a gridview that displays all transactions of a merchant, including pending, approved, and rejected transactions. The gridview is set up with template and includes two buttons. My goal is to prevent the page from reloading when one ...

Unable to resize static and draggable elements simultaneously

Observe this fiddle to see the demonstration: DEMO If you are interested in the resizing of white divs, check out this link: Resizing of White divs The draggable divs are represented by red while the static divs are shown in white and located in droppabl ...

Unable to navigate to the frame within the Salesforce Lightning interface

Attached below is the screenshot and code that I have used: driver.switchTo().frame(driver.findElement(By.xpath("//iframe[contains(@title,'Deploy Data Set')]"))); <div class="slds-template_iframe slds-card" force-aloha-page_aloha-page=""> ...

Remove the most recent file in a MongoDB collection using a DELETE request

As I delve into the world of REST APIs, one task on my to-do list is to delete the last POST in my mongoDB collection using a DELETE route with mongoose. Despite searching for answers, none seem to provide guidance on achieving this deletion through a rout ...

Vue 2 checkbox form array data

Creating a checkbox list with a dynamic id and name: Data: yards[{id:1,name:'test'}] etc HTML: <ul class="checkbox-list"> <template v-for="(yard, index) in yards"> <li> ...

Updating an Angular directive with dynamically added ng-module and ng-change attributes

Can someone check if I'm on the right track? Preamble: I've created a Typeahead class that fetches data and keeps it within itself. The class has the following structure: input: stores the search text. list: stores the results. change: a funct ...

Tips for importing a JavaScript file from a URL and initializing a class in a React component

I am looking to incorporate the PitchPrint app into a React website. They offer a tutorial for vanilla HTML/JS integration here. Following their instructions, I included script tags with links to jQuery and their app file in my index.html file. I then crea ...

The use of a script redirect in PHP can result in a recursive

Hey there, I'm a new rank newbie here! So I have this code that's supposed to redirect users to the relevant page on both mobile and desktop. But it seems like it's causing a never-ending loop with the webpage constantly reloading. Should I ...

Assassin eradicated from list of cast members

In my quest to select a killer from the Cast Members Array for the game, I want the function to pick one person as the killer and then remove them from the castMember player array. Once the killer is chosen, they should be removed from the survivors array ...

Creating packaging for a node-webkit application

https://github.com/rogerwang/node-webkit/wiki/How-to-package-and-distribute-your-apps When I was packaging my node-webkit application for Windows using the instructions provided in the above link, I encountered a challenge. I could not figure out how to p ...

The alert box for model validation summary errors is deactivated when hidden using .hide() method

In my MVC web application form, there is an optional postcode finder. If there is no match for the entered postcode, I add a custom error message to the .validation-summary-errors section. After the error is fixed, I remove all instances of the postcode cl ...

What is the method for reaching a service in a different feature module?

Currently, I am utilizing Angular 2/4 and have organized my code into feature modules. For instance, I have a Building Module and a Client Module. https://i.stack.imgur.com/LvmkU.png The same structure applies to my Client Feature Module as well. Now, i ...

Is there a way to position two Grid elements to the left and one to the right in Material UI, especially if the first Grid element has a specified width

I'm currently using Material UI and have a Grid layout with three items. The left item needs to have a fixed width of 240px and be aligned to the left. The middle item should be left justified and can have any width, containing buttons that I've ...

Is it possible to send array data in a jQuery AJAX call using the GET method?

Is there a way to pass a JavaScript array as a parameter in an AJAX GET request? I am currently dealing with an API call and have a method that requires three parameters, with the third one being an array. ...