0 votes
in Education by (1.7m points)
I wrote the following code to Calculate determinant of a nXn matrix. I could successfully check if the order (i.e. n )entered by the user is valid or not. However I am stuck now: If I enter 3, I must display 9 text boxes and each must be checked. Firstly how do i display in general n^2 text boxes(i.e. n rows and n columns). Secondly how I check each of them individually(i.e. it is a valid entry).

code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Javascript App</title>

<script type="text/javascript" src="app1.js">

</script>

</head>

<body><h1 id="heading1" style="text-align:center; height:auto; width:auto; font-family:'Arial Black', Gadget, sans-serif">Determinant of a nxn matrix</h1>

<p id="paragraph1" style="font-family:'Arial Black', Gadget, sans-serif"> This program allows you to compute the determinant of a nxn matrix</p>

<p>

Input the order of the matrix

<br />

<input type="text" maxlength="3" name="fname" id="value" />

<input type="button" value="submit" onclick="verifyorder()" />

</p>

<p id="error"></p>

<p id="detspace">

<form method="post" action="displaydet()" name="matrix">

</p>

</body>

</html>

javascript:

var order;

function verifyorder(){

    order=document.getElementById('value').value;

    if(order>0){

    displaydet();

    }

    else{

        alert("Sorry, you need to enter a positive integer value, try again");

    document.getElementById('error').innerHTML="Sorry, you need to enter a positive integer value, try again";

    }

    }

JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by (1.7m points)
You will need to dynamically generate the text boxes since you don't know how many there will be in advance. Use the Document Object Model (DOM) for this, or alternatively, use a framework like JQuery which makes the DOM easier to use.

In this case, you need to create n * n text fields, and store them in a grid in two distinct senses of "grid". Firstly, you will want to store them in a physical HTML table, so the user sees them as a grid (that answers question 1). Secondly, you will want to store them in a 2D JavaScript array, so your code can manipulate them (that answers question 2).

So, use document.createElement("input") to create each text field. Use the setAttribute method to set the attributes of the text field, such as its "value" attribute to store the text inside it (these are the same attributes as you would see in the static HTML source for a text field). Then, store the text fields in a 2D array so you can refer back to them later. You should also be dynamically creating an HTML table (using the DOM), dynamically creating rows (tr) and cells (td), and then inserting into each td the generated text field.

Once you have done all that, the user can see all the text fields. When you need to validate them, just refer back to the 2D array you created. Use getAttribute("value") on each of the text fields to get their value back as a string. Then you can validate it as you like.
...