Displaying specific no. of HTML input fields using javascript - javascript

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";
}
}

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.

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript" language="javascript">
function validation_reg()
{
var f1=document.form1;
var vname=f1.Login_Name.value;
var Password=f1.Password.value;
var submitv=f1.submita.value;
{
alert(vname);
alert(password);
}
}
</script>
</head>
<body>
<form name="form1" id="form1" action="" method="POST">
<table align="center" border="1" cellpadding="5">
<tr>
<td>Login Name*</td>
<td><input id="cname" input name="Login_Name" size="30" type="text" title="Your login name, please!" class="required" maxlength="20" /></td>
</tr>
<tr>
<td> Password*</td>
<td><input id="cpaswd" input name="Password" size="31" type="password" maxlength="12" title="Your Password, please!" class="required password" />
<br /></td>
</tr>
<tr>
<td><input class="submit" name="submita" type="submit" value="Submit" onclick="return validation_reg()"/></td></tr>
</table>
</form>
</body>
</html>

Related

HTML maxlength attribute is not working for composition event in Chrome

With composition input (input with IME) for plain text input field in HTML I can input length exceeding max length. After focusing out the value is truncated. But in Chrome the value is left-shifted as shown in the image.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>demo</title>
</head>
<body>
<input id="text" placeHolder="Maximum support 5" maxlength="5" />
</body>
</html>
Is there any way to limit input length in the case of composition input (IME input)?
Use the max attribute instead. It will specify the highest possible number that you may insert
Example:
<input type="number" min="1" max="99999" />
That is weird. As you can see here it works:
<input id="text" placeHolder="Maximum support 5" maxlength="5" />
You can try this:
Remove the placeholder.
Add a type='text' in your input.
Try in a different browser.
Ensure that you're reloaded the code/tab/browser.
Clear cache.

Adding search parameters to url while using clickTag for HTML5 display ad

I building out an HTML5 banner/display ad from scratch and would like to have an html form that searches a site by appending parameters to the url. The user will have a keyword and location field and using an html form with the method="get", the url structure will be
https://jobs.talbots.com/index.gp?method=cappportal.showPortalSearch&sysLayoutId=123&page=1&keyword=test
In order for the ad server to track clicks on the ad, a clickTag query string parameter must be used. This is where I'm having trouble getting the html form to append the search parameters properly.
I have gotten the search to work fine without the use of the clickTag parameter. I have also tried using javascript:window.open(window.clickTag) as the form action along with the script:
<script type="text/javascript">
var clickTag = "https://jobs.talbots.com/index.gp";
</script>`
The parameters seem to fall off using this method. My javascript is not strong enough to write something custom to append the parameters received from the form. Here is documentation the ad server provides: https://dspsupport.basis.net/hc/en-us/articles/115007296827
I'm not sure if it relates to this scenario.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="ad.size" content="width=300,height=250">
<meta name="click.through" content="ad" />
<link rel="stylesheet" href="Talbots_JobSearch_300x250.css"/>
<title>Tablots | Search Jobs - 300x250</title>
<script type="text/javascript">
var clickTag = "https://jobs.talbots.com/index.gp";
</script>
</head>
<body>
<div id="ad" class="wrap">
<form action="javascript:window.open(window.clickTag)" method="get" target="_blank">
<input id="method" type="hidden" name="method" value="cappportal.showPortalSearch">
<input id="sysLayoutID" type="hidden" name="sysLayoutID" value="123">
<label for="keyword">Keyword:</label>
<input id="keyword" type="text" name="keyword"><br>
<input type="submit" value="Search">
</form>
</div>
</body>
</html>
I would like to use a clickTag query string parameter and also append parameters to the url via the html form. This will render a valid search results page.
In my current version I only have a keyword input but will add a location dropdown after.
Instead of using window.open, you can rely on target="_blank" and just use all form input values and a form action to the correct URL.
<form action="https://jobs.talbots.com/index.gp" method="get" target="_blank">
<input id="method" type="hidden" name="method" value="cappportal.showPortalSearch">
<input id="sysLayoutID" type="hidden" name="sysLayoutID" value="123">
<label for="keyword">Keyword:</label>
<input id="keyword" type="text" name="keyword"><br>
<input type="submit" value="Search">
</form>

Display form field value on another html page, after submit

I really need help... :-) I have two html pages and forms on both of them. I need solution to show field value from first form (on first page) in field on second form, after click on SEND button. But, I need both pages to stay open, on first I need to insert values and on second page that values should be shown. Also, I need a solution without PHP because this app will be running on local devices, without server. I don't have any solution and please, I need some tips how I can do that.
I found here some solutions with localStorage and it looks interested but I need to stay on First page and send field value to Second page, in live. There is code which I found here, this is example what I need, just I need both pages to be open and after I insert some value in field on first page and click on button, that value should be shown in field on second page. Thank you in advance!
<html>
<head>
<title>First Page</title>
<script type="text/javascript">
function saveVal() {
var inputFirst = document.getElementById("name").value;
localStorage.setItem("name", inputFirst);
inputFirst = localStorage.getItem("name");
}
</script>
</head>
<body>
<form action="secondPage.html" method="get">
<label>
First Name:
<input name="name" size="20" maxlength="25" type="text" id="name" />
</label>
<input type="submit" value="submit" onclick="saveVal()"/>
</form>
</body>
</html>
<html>
<head>
<title>Second Page</title>
</head>
<body>
<form action="#">
<label>
First Name:
<input name="name" size="20" maxlength="25" type="text" id="name"
readonly="readonly" />
</label>
<script type="text/javascript">
var storedVal = document.getElementById("name");
storedVal.value = localStorage.getItem("name");
</script>
</form>
</body>
</html>
You can use setInterval to constantly check and set the value:
//this will run storeCheck function every 250 miliseconds and update values from localStorage.
setInterval(storeCheck, 250);
function storeCheck() {
var storedVal = document.getElementById("name");
storedVal.value = localStorage.getItem("name");
}

make table rows and following cells as mandatory fields

<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<table>
<tr>id</tr>
<input type="text" id= "101" style = "color:black" required></td>
<input type="button" value="Submit" style = "color:Red;">
<script>
document.getElementById("101").reset()
</script>
</body>
</html>
1) i want to make mandatory my id field but i am unable to do it .I used required but it is not working
2) i want to empty my text fields after submitting the fields when user clicks submit it is not working
3)In my code i used tr as text fields plese suggest for following tr tag only
You need to keep your code inside <form> and just submit type is enough to reset it. No need to write javascript to reset. Here is your working code snippet:
<body>
<form>
<table>
<tr>
<td>id</td>
<td><input type="text" id= "101" style = "color:black" required></td>
<td><input type="submit" value="Submit" style = "color:Red;"></td>
</tr>
</table>
</form>
</body>
If you want separate reset button to reset the field, add this in your table:
<td><input type="reset" value="Reset" style = "color:Red;"></td>
First, your html markup is wrong (inside TR you need to put TD), plus you are missing the form tag.
Adding the form tag, the page will submit and refresh. Te text field will be blank by default, no need to use javascript.
"required" is a HTML5 tag, it will work only with html5 browser that support it.
This code will work with every compatible html5 browser
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<form>
<table>
<tr>
<td>id</td>
<td><input type="text" id= "101" style = "color:black" required></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Submit" style = "color:Red;"></td>
</tr>
</table>
</form>
</body>
</html>
If you want an "editable grid" i.e. a table like structure that allows you to make any of the rows a form, use CSS that mimics the TABLE tag's layout: display:table, display:table-row, and display:table-cell.
There is no need to wrap your whole table in a form and no need to create a separate form and table for each apparent row of your table.
Try this instead:
<style>
DIV.table
{
display:table;
}
FORM.tr, DIV.tr
{
display:table-row;
}
SPAN.td
{
display:table-cell;
}
</style>
<div class="table">
<form class="tr" method="post" action="blah.html">
<span class="td"><input type="text"/></span>
<span class="td"><input type="text"/></span>
</form>
<div class="tr">
<span class="td">(cell data)</span>
<span class="td">(cell data)</span>
</div>
</div>
The problem with wrapping the whole TABLE in a FORM is that any and all form elements will be sent on submit (maybe that is desired but probably not). This method allows you to define a form for each "row" and send only that row of data on submit.
The problem with wrapping a FORM tag around a TR tag (or TR around a FORM) is that its invalid HTML. The FORM will still allow submitting as usual but at this point, the DOM is broken. Note: Try getting the child elements of your FORM or TR with JavaScript, it can lead to unexpected results.
Note that IE7 doesn't support these CSS table styles and IE8 will need a doctype declaration to get it into "standards" mode: (try this one or something equivalent)
Any other browser that supports display:table, display:table-row and display:table-cell should display your CSS data table the same as it would if you were using the TABLE, TR and TD tags. Most of them do.
Note that you can also mimic THEAD, TBODY, TFOOT by wrapping your row groups in another DIV with display: table-header-group, table-row-group, and table-footer-group respectively
you are missign a semi-colon in your input field. Should be
<input type="text" id="101" style="color:black;" required>
In general it's preferable to create a separate .css file rather than utilizing the style attirbute
In general you are invokingthe javascript incorrectly.
<input type="button" value="Submit" style = "color:Red;" onclick="clearInput()">
And you should change the javascript accordingly:
<script>
function clearInput() {
document.GetElementById('101').value = "";
}
</script>
Your table form in general is wrong. Your general structure should look something like this
<table>
<td>
<tr>1</tr>
<tr>2</tr>
</td>
<td>
<tr>Column 2 Row</tr>
</td>
</table>
A working js fiddle example:
function clearInput() {
document.getElementById('101').value = "";
}
input[type="button"] {
color: red;
}
<input type="text" id="101">
<input type="button" value="Submit" onclick="clearInput()">

Tap should skip one of the field in html

Inside a div i have a input field and link , and next to that i have another input field
so by default on pressing tab from input field1 focus goes to link . but i want to skip link from being focused on pressing tab, i,e from input field one if i press tab focus should be in input field2.
is there any html way of doing in. i don't want javaScipt/jQuery
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Tab demo</title>
</head>
<body>
<div>
<input type="text" name ="name1" value ="">
<div>
Add name
</div>
<input type="text" name ="name2" value ="">
</div>
</body>
</html>
reference Link here
This should solve it, using tabindex="-1". W3C HTML5 specification
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Tab demo</title>
</head>
<body>
<div>
<input type="text" name ="name1" value ="">
<div>
Add name
</div>
<input type="text" name ="name2" value ="">
</div>
</body>
</html>
Try adding attribute
tabindex
<input type="text" name ="name1" value ="" tabindex="1" >
<input type="text" name ="name2" value ="" tabindex="2" >
If this will work You can even manipulate in html order of tab jumping between inputs :)
Example here :)

Categories

Resources