Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You have a server-side script called “Ampersands.js” that cannot handle any ampe

ID: 3864237 • Letter: Y

Question

You have a server-side script called “Ampersands.js” that cannot handle any ampersands (&) in the form data. Write a script to convert all ampersands to and in the form field when the field loses focus element (blur).

A) Your script contains one (01) variable and two (02) functions:

1. Global variable: dom.

2. The function getElementAmpersand() that reads the field element by using the existing function getElementById() to access to the element using its id (field) specified in your HTML file by using the function addEventListener() to register the event handler, which takes three (03) arguments :

(a) the name of event as a string literal (here use "blur"), (b) the handler function convertAmpersands, and (c) the Boolean value false.

3. The function "convertAmpersands()" that converts all ampersands in the form field to " and " when the field loses focus ("blur").

4. The html document called "Ampersand.html", which invokes the "Ampersand.js" in the head section.

B) At the end of the JavaScript file, finish with this line to fire the load event when a resource and its dependent resources have finished loading: window.addEventListener( "load", getElementAmpersand, false );

Sample output:

Eile Edit View History Bookmarks I ools Help Enter some text that includes at least one & then click outside the input box. SC&SE; Result is Enter some text that includes at least one & then click outside the input box SC and SE

Explanation / Answer

Here is the code :

<html>
<body>

<p><label class="fixed" for="text">Enter some text that includes atleast one & and click outside the textbox</label><br>

<input type="text" id="textValue"
placeholder="Enter Text"></p>
<br>
<H2>
Result :
</H2>
<p><label class="fixed" for="text">Enter some text that includes atleast one & and click outside the textbox</label><br>
<input type="text" id="resultValue"
placeholder="Enter Text"></p></body>

</html>

Include JS file using script :

var dom;
  
function getElementAmpersand() {


   var txtbox = document.getElementById("textValue");

txtbox.addEventListener("blur", function(){
convertAmpersands();
}, false);
               var value = txtbox.value;
  
  
}
  
function convertAmpersands() {
  
var newValue = document.getElementById("textValue").value.replace(/&/g, "and");
document.getElementById("resultValue").value = newValue;
               }

window.onload = function(){
//you page will probably twitch upon executing code at this time
//but you will almost never have a bad dom call
var txtbox = document.getElementById("textValue");
  
getElementAmpersand();
};

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote