SOLVED: SEE "CORRECTED SOLUTION" Area (below)
--------------------------------- ORIGINAL TEXT ---------------------------
I've created a JavaScript toaster popup to show info about an individual row. It works great in FireFox...but it goes down in flames in IE.
...suggestions and help appreciated.
WHAT I THINK IS FAILING:
I think I declared the "Toaster" class wrong and IE isn't doesn't recognize it as an array. As such, when I call "PopUp(clientId)" on the toaster it fails becuase it doesn't traverse the array.
Additionally, FireFox populates the array correctly in the documents "ready" function...but I'm not sure IE is doing so.
Lastly, since I am still new to JavaScript it (obviously) could be HOW I am creating the classes (as well).
WHERE IT FAILS:
The code fails because the value of "target" is null. This variable is null because IE doesn't seem to traverse the Toaster (array).
this.PopUp = function(clientId) {
var target = null;
jQuery.each(this, function() {
// Hide previous
if (jQuery(this)[0].IsUp)
jQuery(this)[0].PopDown();
if (jQuery(this)[0].ClientId == clientId)
target = jQuery(this)[0];
});
// Show current
target.PopUp();
}
THE FULL CODE SET:
Thanks for the help...
<!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>
<title></title>
<script src="../Includes/JavaScript/jQuery/Core/jquery-1.3.2.js" type="text/javascript"></script>
<style type="text/css">
.toast
{
width: 100%;
position: relative;
}
.toastBorder
{
width: 100%;
position: absolute;
border-radius-topright:.5em;
border-radius-topleft:.5em;
-moz-border-radius-topright:.5em;
-moz-border-radius-topleft:.5em;
-webkit-border-radius-topright:.5em;
-webkit-border-radius-topleft:.5em;
background-color: #FFFFCC;
border: 1px solid #969696;
border-bottom-color: White;
display:none;
height: 0;
bottom: 0;
}
.toastForm
{
display:none;
}
</style>
<script type="text/javascript">
// CLASS DEFINITION:
function Toast(clientId, height, speed) {
// PROPERTIES
this.IsUp = false;
this.IsDown = false;
this.ClientId = clientId;
this.Height = height;
this.Speed = speed;
// METHODS
this.PopUp = function() {
if (this.IsUp) { return; }
this.IsUp = true;
this.IsDown = false;
var speed = this.Speed;
var action = '+=' + this.Height + "px";
// Border
jQuery('#' + this.ClientId).children('div.toastBorder').fadeIn('fast', function() {
jQuery(this).animate({ height: action }, speed, function() {
// Form
jQuery(this).children('div.toastForm').fadeIn('fast');
});
});
}
this.PopDown = function() {
if (this.IsDown) { return; }
this.IsUp = false;
this.IsDown = true;
var speed = this.Speed;
var action = '-=' + this.Height + "px";
// Form
jQuery('#' + this.ClientId).children('div.toastBorder').children('div.toastForm').fadeOut('fast');
// Border
jQuery('#' + this.ClientId + ' div.toastBorder').animate({ height: action }, speed, function() {
jQuery(this).fadeOut('fast');
});
}
}
// CLASS DEFINITION:
function Toaster() {
// PROPERTIES
// METHODS
this.PopUp = function(clientId) {
var target = null;
jQuery.each(this, function() {
// Hide previous
if (jQuery(this)[0].IsUp)
jQuery(this)[0].PopDown();
if (jQuery(this)[0].ClientId == clientId)
target = jQuery(this)[0];
});
// Show current
target.PopUp();
}
this.PopDown = function(clientId) {
jQuery.each(this, function() {
if (jQuery(this)[0].ClientId == clientId)
jQuery(this)[0].PopDown(); // Hide current
});
}
this.Add = function(toast) {
var found = false;
// No duplicates are allowed
jQuery.each(this, function() {
if (jQuery(this)[0].ClientId == toast.ClientId)
found = true;
});
if (!found)
this.push(toast);
}
}
// CLASS DEFINITION: Toaster inheritance
Toaster.prototype = new Array();
var myToaster;
var myToast;
// DOM EVENT: Document.Ready()
jQuery(document).ready(function() {
if (myToaster == null)
myToaster = new Toaster();
myToaster.Add(new Toast("row1", 100, 200));
myToaster.Add(new Toast("row2", 100, 200));
myToaster.Add(new Toast("row3", 100, 200));
myToaster.Add(new Toast("row4", 100, 200));
myToaster.Add(new Toast("row5", 100, 200));
// These BOTH return true in IE...
//alert(myToaster.Items instanceof Array);
//alert(myToaster instanceof Array);
// This is for the button example
if (myToast == null)
myToast = new Toast("row3", 100, 200);
});
</script>
</head>
<body>
<br />
I need help on the following:
<ul>
<li>
This works GREAT in FireFox
</li>
<li>
IE doesn't seem to recognize the Toaster class as being an array.
</li>
</ul>
<div style="width: 300;">
<label style="display:block;color: #660000;">INDIVIDUAL pieces of toast work fine in IE:</label>
<input type="button" value="Down (row 3)" onclick="myToast.PopDown();" />
<input type="button" value="Up (row 3)" onclick="myToast.PopUp();" />
</div>
<br /><br />
<label style="color: #660000">Clicking a row IS SUPPOSED TO toggle a "piece of toast" for that row.</label>
<br /><br />
<table cellpadding="0" cellspacing="0" width="500" style=" border: solid 1px black">
<tr>
<td align="center" style="border-bottom: solid 1px black;">
Header
</td>
<td align="center" style="border-bottom: solid 1px black;">
Header
</td>
<td align="center" style="border-bottom: solid 1px black;">
Header
</td>
<td align="center" style="border-bottom: solid 1px black;">
Header
</td>
<td align="center" style="border-bottom: solid 1px black;">
Header
</td>
</tr>
<tr>
<td>
</td>
<td colspan="3">
<div id="row1" class="toast">
<div class="toastBorder">
<div align="center" class="toastForm">
<br />
Hello
<br /><br /><br /><br /><br />
</div>
</div>
</div>
</td>
<td>
</td>
</tr>
<tr onclick="myToaster.PopUp('row1');">
<td align="center">
Data
</td>
<td align="center">
Data
</td>
<td align="center">
Data
</td>
<td align="center">
Data
</td>
<td align="center">
Data
</td>
</tr>
<tr>
<td>
</td>
<td colspan="3">
<div id="row2" class="toast">
<div class="toastBorder">
<div align="center" class="toastForm">
<br />
Hello
<br /><br /><br /><br /><br />
</div>
</div>
</div>
</td>
<td>
</td>
</tr>
<tr onclick="myToaster.PopUp('row2');">
<td align="center">
Data
</td>
<td align="center">
Data
</td>
<td align="center">
Data
</td>
<td align="center">
Data
</td>
<td align="center">
Data
</td>
</tr>
<tr>
<td>
</td>
<td colspan="3">
<div id="row3" class="toast">
<div class="toastBorder">
<div align="center" class="toastForm">
<br />
Hello
<br /><br /><br /><br /><br />
</div>
</div>
</div>
</td>
<td>
</td>
</tr>
<tr onclick="myToaster.PopUp('row3');">
<td align="center">
Data
</td>
<td align="center">
Data
</td>
<td align="center">
Data
</td>
<td align="center">
Data
</td>
<td align="center">
Data
</td>
</tr>
<tr>
<td>
</td>
<td colspan="3">
<div id="row4" class="toast">
<div class="toastBorder">
<div align="center" class="toastForm">
<br />
Hello
<br /><br /><br /><br /><br />
</div>
</div>
</div>
</td>
<td>
</td>
</tr>
<tr onclick="myToaster.PopUp('row4');">
<td align="center">
Data
</td>
<td align="center">
Data
</td>
<td align="center">
Data
</td>
<td align="center">
Data
</td>
<td align="center">
Data
</td>
</tr>
<tr>
<td>
</td>
<td colspan="3">
<div id="row5" class="toast">
<div class="toastBorder">
<div align="center" class="toastForm">
<br />
Hello
<br /><br /><br /><br /><br />
</div>
</div>
</div>
</td>
<td>
</td>
</tr>
<tr onclick="myToaster.PopUp('row5');">
<td align="center">
Data
</td>
<td align="center">
Data
</td>
<td align="center">
Data
</td>
<td align="center">
Data
</td>
<td align="center">
Data
</td>
</tr>
</table>
</body>
</html>
HERE IS THE CORRECTED SOLUTION:
// CLASS DEFINITION:
function Toast(clientId, maxHeight, speed) {
// PROPERTIES
this.ClientId = clientId;
this.MaxHeight = maxHeight;
this.Speed = speed;
// METHODS
this.IsUp = function() {
return (jQuery('#' + this.ClientId).children().height() > 0) ? true : false;
}
this.PopUp = function() {
if (this.IsUp()) { return; }
var speed = this.Speed;
var action = '+=' + this.MaxHeight + "px";
// Border
jQuery('#' + this.ClientId).children('div.toastBorder').fadeIn('fast', function() {
jQuery(this).animate({ height: action }, speed, function() {
// Form
jQuery(this).children('div.toastForm').fadeIn('fast');
});
});
this.IsUp(true);
}
this.PopDown = function() {
if (!this.IsUp()) { return; }
var speed = this.Speed;
var action = '-=' + this.MaxHeight + "px";
// Form
jQuery('#' + this.ClientId).children('div.toastBorder').children('div.toastForm').fadeOut('fast');
// Border
jQuery('#' + this.ClientId).children('div.toastBorder').animate({ height: action }, speed, function() {
jQuery(this).fadeOut('fast');
});
this.IsUp(false);
}
}
// CLASS DEFINITION:
function Toaster() {
// PROPERTIES
this.Items = new Array();
// METHODS
this.PopUp = function(clientId) {
var target = null;
jQuery.each(this.Items, function() {
if (jQuery(this)[0].ClientId != clientId) {
if (jQuery(this)[0].IsUp()) {
jQuery(this)[0].PopDown(); // Hide previous
}
}
if (jQuery(this)[0].ClientId == clientId) {
target = jQuery(this)[0];
}
});
if (target != null) {
if (target.IsUp() == false)
target.PopUp();
}
}
this.PopDown = function(clientId) {
jQuery.each(this.Items, function() {
if (jQuery(this)[0].ClientId == clientId)
jQuery(this)[0].PopDown(); // Hide current
});
}
this.Add = function(toast) {
var found = false;
// No duplicates are allowed
jQuery.each(this.Items, function() {
if (jQuery(this.Items)[0].ClientId == toast.ClientId)
found = true;
});
if (!found)
this.Items.push(toast);
}
}
var myToaster;
var myToast;
// DOM EVENT: Document.Ready()
$j(document).ready(function() {
if (myToaster == null)
myToaster = new Toaster();
myToaster.Add(new Toast("row1", 125, 200));
myToaster.Add(new Toast("row2", 125, 200));
myToaster.Add(new Toast("row3", 125, 200));
myToaster.Add(new Toast("row4", 125, 200));
myToaster.Add(new Toast("row5", 125, 200));
});
</script>
The attempt to "subclass" Array by using it as the prototype object for your "Toaster" class is not going to work in IE, for whatever reason. If you debug the "Add" method, you'll notice that calls to "push" don't seem to fail, but they also don't change the length of the Toaster.
Trying to make Javascript act like a nice polite object-oriented language is fraught with peril, particularly when you try and treat the native types (like Array) as if they're implemented in Javascript too.
I'd just keep an array around as part of "Toaster" someplace.
Related
I got few buttons question and would like to change the txt color after the value changed. ex: begin with "select" --> changes to "positive" (answer).
where should I make the loop to check if the value was changed? how?
I'm stuck on this code and any help will be good.
var answers = ['Positive', 'Negative'];
if (typeof ARRAY_OF_QUESTIONS[i].header == 'undefined') {
$('#questionsTable tbody').append(Utils.processTemplate("#rowTemplate tbody", data));
$("#" + id + "-inspectionResult").text(data.inspectionResult || 'Select');
$("#" + id + "-inspectionResult").click(resultHandler.bind(data));
updateActiveStatus(data);
commentvisibilitymanager(data);
if(ARRAY_OF_QUESTIONS[i].header == 'undefined'){
$("#" + id + "-inspectionResult").text(data.inspectionResult || 'Select').addClass(asnwers)
} // loop check for value
if (asnwers) {
}
}
else {
$('#questionsTable tbody').append(Utils.processTemplate("#sectionRowTemplate tbody", data));
}
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table frame="box" id="questionnaireHeader" width="100%'" position="fixed">
<tr height="40px" style="background: #006b54; color:white" >
<td width="70%" align="center" style="align:center">Question</div>
<td width="30%" align="center" style="align:center">Result</td>
</tr>
</table>
<table frame="box" width="100%" id="questionsTable">
<tbody>
</tbody>
</table>
<!-- This hosts all HTML templates that will be used inside the JavaScript code -->
<table class ="cls-{id} active-{active}" style="display:none;" width="100%" id="rowTemplate">
<tr class ="bb cls-{id} active-{active}">
<td class="active-{active}" id="{id}-question" width="70%">{question}</td>
<td class="cls-{id} active-{active}" style="border-style:hidden; font-family: 'Poppins', sans-serif !important;" width="30%">
<button class="buttons" step="0.01" data-clear-btn="false" style="background: #006b54; border radius: px; color:white !important ;" id="{id}-inspectionResult"></button>
</td>
</tr>
</table>
<div id="projectPopUp" class="popup-window" style="display:none;">
<div class="popuptitle" id="details-name"></div>
<table id="detailsgrid">
<tbody></tbody>
</table>
<div>
<button class="smallButton" onClick="closePopup()">Close</button>
</div>
</div>
<table style="display:none;" id="popupPersonTemplate">
<tr class ="bb cls-{id}">
<td width="70%">{name}</td>
<td width="30%">
<button class="buttons" step="0.01" data-clear-btn="false" style="background: #006b54; color:white !important ;" id="{id}-status"></button>
</td>
</tr>
</table>
<!--closeProjectPopup()-->
<table style="display: none;" id="sectionRowTemplate">
<tr width="100%" class="bb cls-{id}-row2 sectionheader">
<td class="cls-{id}" colspan="3">{question}</td>
</tr>
</table>
its done!
var checkComplete = true;
var questionResults = data.employee_results;
for (var employee in questionResults){
if (questionResults[employee] == ''){
checkComplete = false;
}
}
if (checkComplete) {
//button
$("#" + id + "-inspectionResult").text('Done');
}
else {
$("#" + id + "-inspectionResult").text('Select');
}
}
else {
$('#questionsTable tbody').append(Utils.processTemplate("#sectionRowTemplate tbody", data));
}
Good evening! I'm teaching myself to code. Right now, I'm making a JavaScript loan calculator, but I hit a snag. If I put 0% interest, it displays nothing in my output textboxes. Everything else is working perfectly though. Any help would be appreciated. Thanks!
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Loan Calculator</title>
<style type="text/css">
.auto-style1 {
text-align: left;
}
.auto-style2 {
font-size: larger;
color: #FFF;
font: Georgia;
}
.auto-style3 {
width: 82px;
text-align: center;
style="float: right;
}
table {
background-color: #F5F5F5;
width: 400px;
height: 300px;
padding-left: 20px;
padding-bottom: 20px;
padding-top: 20px;
}
body {
background-color: #d2691e;
}
</style>
</head>
<body>
<form name="loaninfo">
<div class="auto-style1">
<p><strong>
<span class="auto-style2"> </span></strong><strong><span class="auto-style2"><br />
</span></strong></p>
</div>
<table width="327">
<tr><td colspan="3"></td></tr>
<tr>
<td>Loan Amount:</td>
<td>
$
<input type="text" name="principal" size="12" title="textfield" pattern="([0-9]+\.)?[0-9]+" >
</td>
</tr>
<tr>
<td>Interest Rate:</td>
<td>
<input type="text" name="interest" size="12" title="textfield" pattern="([0-9]+\.)?[0-9]+" >
%
</td>
</tr>
<tr>
<td>Number of Years for Loan:</td>
<td>
<input type="text" name="years" size="12" title="textfield" pattern="([0-9]+\.)?[0-9]+" >
</td>
</tr>
<tr>
<td colspan="3"><input type="button" class="auto-style3" onClick="calculate();" value="Calculate">
<br />
<br />
</td>
</tr>
<tr>
<td colspan="3">
<b>Your Payment Information</b>
</td>
</tr>
<tr>
<td>Monthly Payment Amount:</td>
<td>$ <input type="text" name="payment" size="12" readonly /></td>
</tr>
<tr>
<td>Total Payment Amount:</td>
<td>$ <input type="text" name="total" size="12" readonly ></td>
</tr>
<tr>
<td>Total Interest Payments:</td>
<td>$ <input type="text" name="totalinterest" size="12" readonly /> </td>
</tr>
<tr>
<td colspan="3">
<input type="reset" class="auto-style3" />
</td>
</tr>
</table>
</form>
<script language="JavaScript">
function calculate() {
var principal = document.loaninfo.principal.value;
var months_in_year = 12
var interest = document.loaninfo.interest.value / 100 / months_in_year;
var payments = document.loaninfo.years.value * months_in_year;
var x = Math.pow(1 + interest, payments);
var monthval = (principal*x*interest)/(x-1);
if (!isNaN(monthval) &&
(monthval != Number.POSITIVE_INFINITY) &&
(monthval != Number.NEGATIVE_INFINITY)) {
document.loaninfo.payment.value = round(monthval);
document.loaninfo.total.value = round(monthval * payments);
document.loaninfo.totalinterest.value = round((monthval * payments) - principal);
}
else {
document.loaninfo.payment.value = "";
document.loaninfo.total.value = "";
document.loaninfo.totalinterest.value = "";
}
function round(x) {
return Math.round(x*100)/100;
}
function jsDecimals(e) {
var evt = (e) ? e : window.event;
var key = (evt.keyCode) ? evt.keyCode : evt.which;
if (key != null) {
key = parseInt(key, 10);
if ((key < 48 || key > 57) && (key < 96 || key > 105)) {
if (!jsIsUserFriendlyChar(key, "Decimals")) {
return false;
}
}
else {
if (evt.shiftKey) {
return false;
}
}
}
return true;
}
form.onsubmit = function () {
return textarea.value.match(/^\d+(\.\d+)?$/);
}
</script>
When you put 0 you are getting
var x = Math.pow(1 + interest, payments); // 1
var monthval = principal * x * interest / (x - 1); //NaN because 1-1 in fraction is 0 so it returns 0/0 and it's NaN
you can change you code to be like this:
if (interest===0){
var monthval = (principal)/(months_in_year);
} else {
var monthval = (principal*x*interest)/(x-1);
}
Working Version: http://codepen.io/mhadaily/pen/ZpypdA
feel free to change it to be like what you want.
It's because of this section of code.
else if (interest == 0) {
document.loaninfo.payment.value = "";
document.loaninfo.total.value = "";
document.loaninfo.totalinterest.value = "";
}
When the interest is 0, you are setting the values of all your output boxes to an empty string.
You should replace the RHS of the assignment.
I have a call in the parent HTML which invokes a javascript call.
<div class="data">
<form:input title="Building" styleClass="content contractorDisable" maxlength="5" size="6" path="fireImpairForm.bldCode" />
<a href="javascript:winOpen('LookupLocation.htm','bld')">
<img src="image/search.gif" border="0" alt="Click here to find Building"></a>
</div>
The javascript used for the modal window previously was using a window.showModal where the code has been commented as shown in the javascript code below.
I am looking to have a replacement for this same call through Jquery and using the jQuery dialog code.
and this is the javascript call that is called which encapsulates a jquery dialog popup. Previously I was able to set a value in the parent input text value to whatever was selected in the modalDialog window using the return object of modalwindow.That code is now commented out to show what it was and what the implementation I am looking for in its place. I am looking for some replacement for that implementation using jQuery dialog. My Dailog is encapsulating another jsp which is a table so I am not able to return the value back to the parent form.
function winOpen(urlVal, type) {
var printNames = new Object();
var ind = [document.forms[0].elements["fireImpairForm.statusId"].selectedIndex].value;
ind = document.forms[0].elements["fireImpairForm.facility"].selectedIndex;
var facilityCode = document.forms[0].elements["fireImpairForm.facility"].item(ind).value;
var sub = facilityCode.substring(((facilityCode).indexOf("-")) + 1, (facilityCode).length);
var params = "?bldCode=" + document.forms[0].elements["fireImpairForm.bldCode"].value + "&floorCode=" + document.forms[0].elements["fireImpairForm.floorCode"].value + "&campusCode=" + sub + "&check=" + "check" + "&facilityCode=" + facilityCode;
/*
retObj = window.showModalDialog(urlVal+params,"","scroll:no;status:no;dialogWidth:620px;dialogHeight:600px;unadorned:yes;resizable=yes");
if (retObj != null) {
document.forms[0].elements["fireImpairForm.bldCode"].value=retObj.code;
}
*/
}
var page = urlVal + params;
var $dialog = $('<div></div>')
.html('<iframe style="border: 0px; " src="' + page + '" width="100%" height="100%"></iframe>')
.dialog({
autoOpen: false,
modal: true,
height: 625,
width: 500,
title: "Buildings"
});
$dialog.dialog('open');
}
The LookupLocation.htm spring controller returns a jsp
<BODY topmargin=0 leftmargin=0 bottommargin=0 rightmargin=0>
<center>
<form name="locForm">
<input type="hidden" name="code" />
<DIV style="overflow:auto;clear:both; width:100%; height:525px; border :1px solid;">
<TABLE onkeydown="if (event.keyCode=='13') return returnToParent()" cellpadding="0" cellspacing="1" id="resultTable" width="100%">
<TBODY>
<c:if test="${empty resultList}">
<tr>
<td bgcolor="buttonface"><b>No data found</b></td>
</tr>
</c:if>
<c:if test="${!empty resultList}">
<tr>
<td> </td>
</tr>
<tr>
<td class="label">Search:</td>
<td class="content">
<input name="searchFld" type="text" size=15 onChange="">
</td>
<td class="content">
<input type="button" value="Find" onclick="findString(document.locForm.searchFld.value)">
</td>
</tr>
<tr>
<td nowrap class="searchTableHeader" align="center" STYLE="color:white">Select</td>
<td nowrap class="searchTableHeader" align="center" STYLE="color:white">Location Code</td>
<td nowrap class="searchTableHeader" align="center" STYLE="color:white">Description</td>
</tr>
<c:set var="evenCount" value="${0}" />
<c:forEach var="result" varStatus="i" items="${resultList}">
<c:set var="evenCount" value="${evenCount+1}" />
<c:choose>
<c:when test="${evenCount % 2 == 0}">
<tr id='row${evenCount}' class="even_row">
</c:when>
<c:otherwise>
<tr id='row${evenCount}' class="odd_row">
</c:otherwise>
</c:choose>
<td width="5%" align="center" class="content">
<input type="radio" name="radioValue" onclick='highlight(${evenCount}); setLookupValuesOne("${result.code}");returnToParent();'>
</td>
<td width="15%" nowrap class="content">
<c:out value="${result.code}" />
</td>
<td nowrap class="content">
<c:out value="${result.description}" />
</td>
</tr>
</c:forEach>
</c:if>
</TBODY>
</TABLE>
</DIV>
</form>
</center>
</BODY>
Any help will be much appreciated.
I have looked at several implementation of jQuery Dialogs and I am not able to piece together how I can have parent form interaction between the jQuery Dialog.
UPDATED :adding the rendered HTML requested by Mark
Here is the updated rendered HTML.
Normal behaviour was , when radio button is selected, the window closed and returned the selected object which was set to a input value on the parent form but now the return object is not getting captured and I can not set the input value fireImpairForm.bldCode
<html>
<BODY topmargin=0 leftmargin=0 bottommargin=0 rightmargin=0>
<center>
<form name="locForm">
<input type="hidden" name="code" />
<DIV style="overflow:auto;clear:both; width:100%; height:525px; border :1px solid;">
<TABLE onkeydown="if (event.keyCode=='13') return returnToParent()" cellpadding="0" cellspacing="1" id="resultTable" width="100%">
<TBODY>
<tr>
<td> </td>
</tr>
<tr>
<td class="label">Search:</td>
<td class="content">
<input name="searchFld" type="text" size=15 onChange="">
</td>
<td class="content">
<input type="button" value="Find" onclick="findString(document.locForm.searchFld.value)">
</td>
</tr>
<tr>
<td nowrap class="searchTableHeader" align="center" STYLE="color:white">Select</td>
<td nowrap class="searchTableHeader" align="center" STYLE="color:white">Location Code</td>
<td nowrap class="searchTableHeader" align="center" STYLE="color:white">Description</td>
</tr>
<tr id='row1' class="odd_row">
<td width="5%" align="center" class="content">
<input type="radio" name="radioValue" onclick='highlight(1); setLookupValuesOne("PC ");returnToParent();'>
</td>
<td width="15%" nowrap class="content">PC </td>
<td nowrap class="content">10150 Place</td>
</tr>
<tr id='row2' class="even_row">
<td width="5%" align="center" class="content">
<input type="radio" name="radioValue" onclick='highlight(2); setLookupValuesOne("ON ");returnToParent();'>
</td>
<td width="15%" nowrap class="content">ON </td>
<td nowrap class="content">1019 Building</td>
</tr>
<tr id='row3' class="odd_row">
<td width="5%" align="center" class="content">
<input type="radio" name="radioValue" onclick='highlight(3); setLookupValuesOne("OG ");returnToParent();'>
</td>
<td width="15%" nowrap class="content">OG </td>
<td nowrap class="content">19137 Building</td>
</tr>
<tr id='row4' class="even_row">
<td width="5%" align="center" class="content">
<input type="radio" name="radioValue" onclick='highlight(4); setLookupValuesOne("TO ");returnToParent();'>
</td>
<td width="15%" nowrap class="content">TO </td>
<td nowrap class="content">2011 Building</td>
</tr>
<tr id='row5' class="odd_row">
<td width="5%" align="center" class="content">
<input type="radio" name="radioValue" onclick='highlight(5); setLookupValuesOne("TT ");returnToParent();'>
</td>
<td width="15%" nowrap class="content">TT </td>
<td nowrap class="content">30133 4 nw Street Building</td>
</tr>
<tr id='row6' class="even_row">
<td width="5%" align="center" class="content">
<input type="radio" name="radioValue" onclick='highlight(6); setLookupValuesOne("TH ");returnToParent();'>
</td>
<td width="15%" nowrap class="content">TH </td>
<td nowrap class="content">13939 Warehouse</td>
</tr>
<tr id='row7' class="odd_row">
<td width="5%" align="center" class="content">
<input type="radio" name="radioValue" onclick='highlight(7); setLookupValuesOne("N2 ");returnToParent();'>
</td>
<td width="15%" nowrap class="content">N2 </td>
<td nowrap class="content">40th Avenue Warehouse</td>
</tr>
</TBODY>
</TABLE>
</DIV>
</form>
</center>
</BODY>
</html>
Here the jscript calls if that helps.
I am having a time trying to format the three jscript calls . If you need it I will paste it somehow. They are not really important though for what I am asking. The first is highlighting grey and white background depending on selected row number odd or even. The next ones I have added
function returnToParent() {
//var defer=$.Deferred();
if (document.forms[0].code.value == null || document.forms[0].code.value == "") {
alert("Please select a row.");
return false;
}
var rowObj = new Object();
rowObj.code = document.forms[0].code.value;
if (window.showModalDialog) {
self.returnValue = rowObj;
} else {
//opener.setData(rowObj);
}
//defer.resolve("true");//this text 'true' can be anything. But for this usage, it should be true or false.
//$(window.opener.document).forms['dialogForm'].dailogFormVal.val=document.forms[0].code.value;
//$(window.opener.document).find().val(document.forms[0].code.value);
var parent = $(window.frameElement).parent();
parent.find("#dailogFormVal").val(document.forms[0].code.value);
$(this).dialog("close");
}
function setLookupValuesOne(codeValue) {
document.forms[0].code.value = codeValue;
var parent = $(window.frameElement).parent();
parent.find("#dailogFormVal").val(document.forms[0].code.value);
//$(window.parent.document.getElementById("dailogFormVal")).val(document.forms[0].code.value);
//$(window.opener.document).forms['dialogForm'].getElementById['dailogFormVal'].value=codeValue;
The finally ending "}" is not getting added to the jscript code.
Updated to show the modified jScript
console.clear();$dialog.append("<iframe id='dialogframe' style='border: 0px; width: 100%;height:100% '/>");$dialog.dialog({autoOpen: false, modal: true,width: "auto", height: "auto", title: "Buildings", buttons: [{
text: "Close", click: function() { $(this).dialog('close'); } }]});function winOpenDlg(urlVal, type) { var printNames = new Object(); var ind =[document.forms[0].elements["fireImpairForm.statusId"].selectedIndex].value; ind = document.forms[0].elements["fireImpairForm.facility"].selectedIndex; var facilityCode = document.forms[0].elements["fireImpairForm.facility"].item(ind).value; var sub = facilityCode.substring(((facilityCode).indexOf("-"))+1,(facilityCode).length); if (type == 'floor') { if (document.forms[0].elements["fireImpairForm.bldCode"].value.length <= 0) { alert('Please select a building.'); }else { var params="?bldCode="+document.forms[0].elements["fireImpairForm.bldCode"].value+"&campusCode="+sub+"&facilityCode="+facilityCode;
}
} else if (type == 'room') {
if (document.forms[0].elements["fireImpairForm.bldCode"].value.length <= 0) {
alert('Please select a building.');
}else if (document.forms[0].elements["fireImpairForm.floorCode"].value.length <= 0) {
alert('Please select a floor.');
}else {
var params="?bldCode="+document.forms[0].elements["fireImpairForm.bldCode"].value+"&floorCode="+document.forms[0].elements["fireImpairForm.floorCode"].value+"&campusCode="+sub+"&check="+"check"+"&facilityCode="+facilityCode;
}
} else {
var params="?campusCode="+sub+"&facilityCode="+facilityCode;
}var page = urlVal + params; $('#dialogframe').attr('src', page);// yours would do this // here I create a sample set of text to inject //var sample = '<div id="findem">Hi I am found</div>';var dialogBody = $("#dialogframe").contents().find("body");//$($dialog.find('#dialogframe')[0].contentWindow.document.body);//$('#dialogframe').attr('src', page);// yours would do this // I do this for simplicity and demonstration//dialogBody.html("<div id='original'>First Text </div>"); //dialogBody.append(sample);// add a click event to the dialog contents, you would do different things dialogBody.on('click', '[id^=row]', function() { console.log("triggered !!");
console.log(this.id + ":" + this.innerHTML); // id of element clicked document.forms[0].elements["fireImpairForm.bldCode"]=document.forms[0].elements[this.id].value;}); $dialog.dialog('open');}
Updated to show latest Javascript
I updated my code and removed any reference to my src location but even then the same error comes up.
EditFireImpair.htm?permitIk=301 Uncaught TypeError: Cannot read property 'contentWindow' of undefined
Defined below is the java script code I inserted almost unchanged from what you posted. The dialog does not open up as it does on your fiddle page.
<script>
console.clear();
var $dialog = $('#mydialog');
$dialog.append("<iframe id='dialogframe' style='border: 0px; width: 100%;height:100% '/>");;
$dialog.dialog({
autoOpen: false,
modal: true,
width: "auto",
height: "auto",
title: "Buildings",
buttons: [{
text: "Close Me",
click: function() {
$(this).dialog('close');
}
}]
});
function winOpenNewDlg(urlVal, type) {
var printNames = new Object();
var ind =[document.forms[0].elements["fireImpairForm.statusId"].selectedIndex].value;
ind = document.forms[0].elements["fireImpairForm.facility"].selectedIndex;
var facilityCode = document.forms[0].elements["fireImpairForm.facility"].item(ind).value;
var sub = facilityCode.substring(((facilityCode).indexOf("-"))+1,(facilityCode).length);
if (type == 'floor') {
if (document.forms[0].elements["fireImpairForm.bldCode"].value.length <= 0) {
alert('Please select a building.');
}else {
var params="?bldCode="+document.forms[0].elements["fireImpairForm.bldCode"].value+"&campusCode="+sub+"&facilityCode="+facilityCode;
}
} else if (type == 'room') {
if (document.forms[0].elements["fireImpairForm.bldCode"].value.length <= 0) {
alert('Please select a building.');
}else if (document.forms[0].elements["fireImpairForm.floorCode"].value.length <= 0) {
alert('Please select a floor.');
}else {
var params="?bldCode="+document.forms[0].elements["fireImpairForm.bldCode"].value+"&floorCode="+document.forms[0].elements["fireImpairForm.floorCode"].value+"&campusCode="+sub+"&check="+"check"+"&facilityCode="+facilityCode;
}
} else {
var params="?campusCode="+sub+"&facilityCode="+facilityCode;
}
var page = urlVal + params;
//$('#dialogframe').attr('src', page);// yours would do this
// here I create a sample set of text to inject
var sample = '<div id="findem">Hi I am found</div>';
var dialogBody = $($dialog.find('#dialogframe')[0].contentWindow.document.body);
// $('#dialogframe').attr('src', page);// yours would do this
// I do this for simplicity and demonstration
dialogBody.html("<div id='original'>First Text </div>");
dialogBody.append(sample);
// add a click event to the dialog contents, you would do different things
dialogBody.on('click', '*', function() {
console.log("triggered !!");
console.log(this.id + ":" + this.innerHTML); // id of element clicked
document.forms[0].elements["fireImpairForm.bldCode"]=document.forms[0].elements[this.id].value;
});
$dialog.dialog('open');
}
</script>
and this is the HTML
<div style="display:none; visibility:hidden;">
<div id="mydialog">
<input type="hidden" id="bldCode" />
</div>
</div>
<a href="javascript:winOpenNewDlg('LookupLocation.htm','bld')">
<img src="image/search.gif" border="0"
alt="Click here to find Building"></a>
Ok rather than dig into your details let us start with a very simplified example with some comments: See and play with it here: https://jsfiddle.net/MarkSchultheiss/kkwpb5b7/
EDIT: Note how I put a click event in my code on the dialog content. This is similar to your 'click something/event of something etc) that you wish to do.
I put a simple svg icon in, yours is a gif (so we have something to click).
Markup (for my examples, yours differs a bit)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
<div class="data">
<form:input title="Building" styleClass="content contractorDisable" maxlength="5" size="6" path="fireImpairForm.bldCode" />
<a class='lookuplocation' data-url="LookupLocation.htm" ,data-type="bld">
<img border="0" alt="Click here to find Building">
<svg xmlns="http://www.w3.org/2000/svg" width="200" height="200">
<circle cx="50" cy="50" r="25" stroke="black" stroke-width="5" fill="red" />
</svg>
</a>
</div>
<div style="display:none; visibility:hidden;">
<div id="mydialog">
</div>
</div>
Now see my "hidden" dialog wrapper? let's use that rather than some in-line script and then inject some content into that (you would use the src)
var $dialog ={};
$(document).ready(function()(
$dialog = $('#mydialog');
$dialog.append("<iframe id='dialogframe' style='border: 0px; width: 100%;height:100% '/>");;
$dialog.dialog({
autoOpen: false,
modal: true,
width: "auto",
height: "auto",
title: "Buildings",
buttons: [{
text: "Close Me",
click: function() {
$(this).dialog('close');
}
}]
});
});
function winOpen(urlVal, type) {
var params = '?bldCode=howdy'; // put your stuff in here
var page = urlVal + params;
// $('#dialogframe').attr('src', page);// yours would do this
// here I create a sample set of text to inject
var sample = '<div id="findem">Hi I am found</div>';
var dialogBody = $($dialog.find('#dialogframe')[0].contentWindow.document.body);
// $('#dialogframe').attr('src', page);// yours would do this
// I do this for simplicity and demonstration
dialogBody.html("<div id='original'>First Text </div>");
dialogBody.append(sample);
// add a click event to the dialog contents, you would do different things
dialogBody.on('click', '*', function() {
console.log("triggered !!");
console.log(this.id + ":" + this.innerHTML); // id of element clicked
});
$dialog.dialog('open');
}
// took this out of the markup because I don't like them there.
$('.data').on('click', 'a.lookuplocation', function() {
winOpen($(this).data('url'), $(this).data('type'));
});
I have a textbox and a plus button. When the user clicks on the plus button a new row will added with textbox and minus button, and the text area have underline like this
[ text ] +
text -
--------------------------------
So I tried something like this:
function AddNote() {
var xtbl = document.getElementById("tblMain");
var xrowcount = xtbl.rows.length;
var xrow = xtbl.insertRow(xrowcount);
var xcell0 =xrow.insertCell(0);
var xcell1 = xrow.insertCell(1);
var xcell2 = xrow.insertCell(2);
var newlabel = document.createElement("Label");
newlabel.id = "id" + xrowcount
newlabel.innerHTML = document.getElementById("txtReleaseNote").value;
xcell1.appendChild(newlabel);
var newlabel1 = document.createElement("Label");
newlabel1.id = "lblminus" + xrowcount
newlabel1.innerHTML="<h2>-</h2>"
xcell1.setAttribute("colspan", 2);
xcell1.setAttribute("borderBottom", "1px solid #0000FF");
xcell2.appendChild(newlabel1);
}
<table id="tblMain" align="center" width="100%" cellpadding="0" cellspacing="0" style="table-layout: fixed; text-align: left; margin-top:10px;">
<colgroup>
<col style="width: 50px;">
<col style="width: 145px;">
<col style="width: 350px;">
<col style="width: 100px;">
<col style="width: auto;">
<!-- Use "width: auto;" to apply the remaining (unused) space -->
<col style="width: 50px">
</colgroup>
<tbody>
<tr><td></td><td>Release Notes</td><td><asp:TextBox
id="txtReleaseNote" TextMode="MultiLine" Rows="3" runat="server" MaxLength="15"
Width= "100%" CssClass="TextBoxBorder"></asp:TextBox></td>
<td style="padding-left:15px; Color:RGB(33,88,103);"> <h2 id="lblplus"
onclick="AddNote()" style="cursor: pointer; vertical-align:text-top;" > + </h2> </td> </tr>
</tbody>
</table>
The minus button vertically not equal to the plus symbol. What am I doing wrong?
How assign color for minus symbol?
I am using asp.net 2008 CSS 2.1
This answer is explain how to fix the UI.
The open issue here is how to save it to the server because you are using ASP.NET and this framework doesn't support dynamic inputs by default. You can read the answer here http://forums.asp.net/t/1611284.aspx?How+to+get+value+from+dynamically+added+html+input+
function AddNote() {
var xtbl = document.getElementById("tblMain");
var xrowcount = xtbl.rows.length;
var xrow = xtbl.insertRow(xrowcount);
var xcell0 =xrow.insertCell(0);
var newlabel = document.createElement("Label");
newlabel.id = "id" + xrowcount
newlabel.innerHTML = document.getElementById("txtReleaseNote").value;
var xcell1 = xrow.insertCell(1);
xcell1.setAttribute("colspan", 2);
xcell1.setAttribute("style", "border-bottom:1px solid #0000FF");
xcell1.appendChild(newlabel);
var xcell2 = xrow.insertCell(2);
xcell2.setAttribute('style', 'padding-left:15px; color:RGB(33,88,103);');
var newlabel1 = document.createElement("label");
newlabel1.id = "lblminus" + xrowcount
newlabel1.innerHTML="<h2 style='cursor:pointer;margin:0;' onclick='removeRow(this)'>-</h2>"
xcell2.appendChild(newlabel1);
}
function removeRow(elm) {
var row = elm.parentNode.parentNode.parentNode;
row.parentNode.removeChild(row);
}
<table id="tblMain" align="center" width="100%" cellpadding="0" cellspacing="0" style="table-layout: fixed; text-align: left; margin-top:10px;">
<colgroup>
<col style="width: 50px;">
<col style="width: 145px;">
<col style="width: 350px;">
<col style="width: 100px;">
<col style="width: auto;">
<!-- Use "width: auto;" to apply the remaining (unused) space -->
<col style="width: 50px">
</colgroup>
<tbody>
<tr>
<td></td>
<td>Release Notes</td>
<td>
<textarea id="txtReleaseNote" rows="3" class="TextBoxBorder"></textarea>
<!-- <asp:TextBox id="txtReleaseNote" TextMode="MultiLine" Rows="3" runat="server" MaxLength="15" Width= "100%" CssClass="TextBoxBorder"></asp:TextBox>-->
</td>
<td style="padding-left:15px; color:RGB(33,88,103);">
<h2 id="lblplus" onclick="AddNote()" style="cursor: pointer; vertical-align:text-top;" > + </h2>
</td>
</tr>
</tbody>
</table>
It's work.
Html code :
<table>
<thead>
<tr>
<th>
Text
</th>
<th> <button type="button" data-bind="click: addNewRow" >
+
</button>
</th>
</tr>
</thead>
<tbody data-bind="template:{name:'tableRow', foreach: tableRows}">
</tbody>
<tfoot>
<tr>
<td colspan="4">
</td>
</tr>
</tfoot>
</table>
<script id="tableRow" type="text/html">
<tr>
<td>
<input type="text" style="width:40px;" data-bind="value: number, valueUpdate: 'keyup'" />
</td>
<td>
<button type="button" data-bind="click: function(){ $data.remove(); }">
-
</button>
</td>
</tr>
</script>
knockout.js
function tableRow(number, ownerViewModel) {
this.number = ko.observable(number);
this.remove = function() {
ownerViewModel.tableRows.destroy(this);
}
}
function tableRowsViewModel() {
var that = this;
this.tableRows = ko.observableArray([]);
this.addNewRow = function() {
this.tableRows.push(new tableRow('', that));
}
this.addNewRow();
//dependentObservable to represent the last row's value
this.lastRowValue = ko.dependentObservable(function() {
var rows = that.tableRows();
return rows.length ? rows[rows.length - 1].number() : null;
});
//subscribe to changes to the last row
this.lastRowValue.subscribe(function(newValue) {
if (newValue) {
that.tableRows.push(new tableRow('', that));
}
});
}
$(document).ready(function() {
ko.applyBindings(new tableRowsViewModel());
});
For More visit this:
http://jsfiddle.net/rniemeyer/f5f8s/
On the page there are number of text area. The number is dynamic not fixed. I need to limit length of all text areas on one page. How can I do this in js or jquery?
My try:-
<body>
<div id="contact">
<form action="" method="post">
<fieldset>
<table style="width: 100%;">
<tr class="questionsView" style="width: 100%;margin: 5px;">
<td class="mandatory">
<b>1
*Qywerew we</b>
<hr/>
<table class="profileTable" style="margin-left: 25px;">
<tr>
<td>
<textarea style="border: solid 1px #800000;" rows="5" name="165" cols="100">
</textarea>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
</td>
</tr>
<tr class="questionsView" style="width: 100%;margin: 5px;">
<td class="">
<b>2
a da da da</b>
<hr/>
<table class="profileTable" style="margin-left: 25px;">
<tr>
<td>
<textarea style="border: solid 1px #800000;" rows="5" name="166" cols="100">
</textarea>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
</td>
</tr>
</table>
<input type="submit" value="Submit" onclick="return checkThis()">
</fieldset>
</form>
</div>
<script>
$('textarea').bind('paste keyup blur', function(){
$(this).val(function(i, val){
return val.substr(0, 5);
});
});
</script>
</body>
$('textarea').bind('paste keyup blur', function() {
$(this).val(function(i, val) {
return val.substr(0, 5);
});
});
jsFiddle.
Update
I don't know why but it prints function(i, val) { return val.substr(0, 5); } in text area every time.
Sounds like you are using an older version of jQuery (pre 1.4). The refactored code below will work.
$('textarea').bind('paste keyup blur', function() {
$(this).val(this.value.substr(0, 5));
});
jsFiddle.
The previous code would not work prior to jQuery 1.4 because it expected a string only as the argument to val(). By passing a function, its toString() was implicitly called, returning the string representation of the function.
Horrible and aggressive way
setInterval(function(){
$('textarea').each(function(){
if (this.value.length > 5){
this.value = this.value.substr(0,5);
}
})
}, 1)
http://jsfiddle.net/YMsEF/
var maxLettersCount = 3;
$('textarea').bind('keydown paste', function(event) {
var textarea = $(event.target),
text = textarea.val(),
exceptionList = {
8: true,
37: true,
38: true,
39: true,
40: true,
46: true
};
if (event.type == 'keydown' && text.length >= maxLettersCount && !exceptionList[event.keyCode]) {
return false;
} else if (event.type == 'paste') {
setTimeout(function() {
textarea.val(textarea.val().substring(0, maxLettersCount));
}, 1);
}
})