Using Knockout to trigger events when switching DIVs - html

I'm looking to trigger an event (namely, a save/cancel dialog) when switching focus between text elements--with one caveat: it's not per element, it's per containing div.
I'll have multiple divs, each with the same controls. If any values are changed in one containing div and focus is switched to another, I need to determine if the knockout data I'm leaving is dirty and then trigger the event.
Does knockout support this kind of event binding or will I have to wire something else up? It looks like I could use the tabindex attribute on my divs but I'd prefer to use existing functionality in the framework if it's available.
A mockup of the code would look like this:
<div>
First Name: <input type="text" name="firstName"/><br/>
Last Name: <input type="text" name="lastName"/><br/>
Customer Type: <select>
<option value="Individual">Individual</option>
<option value="Corporate">Corporate</option>
</select>
</div>
<div>
First Name: <input type="text" name="firstName"/><br/>
Last Name: <input type="text" name="lastName"/><br/>
Customer Type: <select>
<option value="Individual">Individual</option>
<option value="Corporate">Corporate</option>
</select>
</div>

how about something like this. kind of jquery / knockout hybrid. clicking on an attribute brings up save button. after save and moving to a different div getting focus brings up save again.
function viewModel() {
var self = this;
this.currentDiv = ko.observable('');
this.isDirty = ko.observable(false);
this.save = function() {
self.isDirty(false)
}
}
var vm = new viewModel();
(function($) {
ko.applyBindings(vm); //bind the knockout model
$("input, select").not('#save').focus(function() {
var d = $(this).parent('div').attr('id');
if (d != vm.currentDiv() || vm.isDirty()) {
vm.isDirty(true);
} else {
vm.isDirty(false);
}
vm.currentDiv(d);
});
})(jQuery);
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="div1"> First Name:
<input type="text" name="firstName" />
<br/> Last Name:
<input type="text" name="lastName" />
<br/> Customer Type:
<select>
<option value="Individual">Individual</option>
<option value="Corporate">Corporate</option>
</select>
</div>
<div id="div2">
First Name:
<input type="text" name="firstName" />
<br/> Last Name:
<input type="text" name="lastName" />
<br/> Customer Type:
<select>
<option value="Individual">Individual</option>
<option value="Corporate">Corporate</option>
</select>
</div>
<input type="button" id="save" data-bind="visible: isDirty, click: save" value="save" />

Related

on click radio button change select value only html css

<!DOCTYPE html>
<html>
<body>
<form action="" id="carform" name="radioForm">
<input type="radio" name="fname" id="Volvo:" value="volvo">
<label for="Volvo:">Volvo:</label>
<input type="radio" name="fname" id="Saab:" value="saab">
<label for="Saab:">Saab:</label>
<input type="radio" name="fname" id="Opel:" value="opel">
<label for="Opel:">Opel:</label>
<input type="radio" name="fname" id="Audi:" value="audi">
<label for="Audi:">Audi:</label>
<br/>
<select form="carform" id="select">
<option name="fname" value="volvo">Volvo</option>
<option name="fname" value="saab">Saab</option>
<option name="fname" value="opel">Opel</option>
<option name="fname" value="audi">Audi</option>
</select>
<br><br>
<input type="submit" />
</form>
<script>
var rad = document.radioForm.fname;
var prev = null;
for(var i = 0; i < rad.length; i++) {
rad[i].onclick = function() {
if(this !== prev) {
prev = this;
}
console.log(this.value);
document.getElementById('select').value=this.value;
};
}
</script>
</body>
</html>
whenever I click on radio button input type, select value is changing, this I have done using javascript. Can anyone help me to do same thing without javascript, jquery and all? Only using html, css.
Not possible with HTML and CSS. HTML is a markup language and CSS is styling the HTML. They don't provide any thing for event base programming. You need to use jquery/javascript for event base programming.
You can only do that using javascript not possible with Html. use react for virtual DOM. Or you can do it simply by using simple JS or jquery.

Converting values on HTML form to a string

<fieldset>
<legend>Making a String</legend>
Test1:<br/>
<input type="text" name="Test1Field"/><br/>
Test2:<br/>
<input type="text" name="Test2Field"/><br/>
Test3:<br/>
<select name="Test3Field">
<option value="test1">0</option>
<option value="test2">1</option>
</select>
<br/>
Test4:<br/>
<select name="Test4Field">
<option value="test1">0</option>
<option value="test2">1</option>
</select>
<br/>
Test5:<br/>
<select name="Test5Field">
<option value="test1">0</option>
<option value="test2">1</option>
</select>
<br/>
<br/>
<button> Submit </button>
</fieldset>
I'm fairly new to HTML and trying to make some simple applications. I want to be able to convert the values that are inserted into this HTML <form> into a string once the submit button is pressed.
For example:
"Test1FieldValue, Test2FieldValue, Test3FieldValue, Test4FieldValue, Test5FieldValue"
Is there any way that this can be done?
You can do this with javascript. Basically, add an eventListener on your button, find all fields (inputs and selects) and create the string. The following will do that for you (I've added some comments for what the code does):
document.getElementById("submit").addEventListener("click", function() { // add an listener to the button
var fieldset = document.getElementById("fieldset"); // find the fieldset
var asArray = Array.from(fieldset.querySelectorAll("input, select")).map(v => { // loop over all found input and select elements
return v.name + ":" + v.value; // return the name and the value
});
console.log(asArray);
var asString = asArray.join(); // join the resulting array to a string
console.log(asString);
})
<fieldset id="fieldset">
<legend>Making a String</legend>
Test1:
<br/>
<input type="text" name="Test1Field" />
<br/> Test2:
<br/>
<input type="text" name="Test2Field" />
<br/> Test3:
<br/>
<select name="Test3Field">
<option value="test1">0</option>
<option value="test2">1</option>
</select>
<br/> Test4:
<br/>
<select name="Test4Field">
<option value="test1">0</option>
<option value="test2">1</option>
</select>
<br/> Test5:
<br/>
<select name="Test5Field">
<option value="test1">0</option>
<option value="test2">1</option>
</select>
<br/>
<br/>
<button id="submit"> Submit </button>
</fieldset>
HTML:
<input type="text" name="textfield1" id="textfield1" value="value" />
JS:
var nameValue = document.getElementById("textfield1").value;

In AngularJS how to use datalist

<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>
<div ng-app="" ng-controller="cntryController">
<input list="testList" type="" ng-model="SelectedDoctor" ng-change="LoadSessionData(SelectedDoctor)" />
<datalist id="testList">
<option value="Dr.Test1" ng-selected="selected"></option>
<option value="Dr.Test2"></option>
<option value="Dr.Test2"></option>
</datalist>
</div>
Controller
function cntryController($scope) {
$scope.LoadSessionData = function(val) {
console.log(val);
};
}
check this Link http://jsbin.com/jifibugeke/1/edit?html,js,console,output
Above mention datalist sample code and URL using angularjs, here my problem is what ever i am typing the text box, in controller add by added every words,in my requirement in datalist selected details only shows in controller,
//here your html
<div ng-app="myapp1" ng-controller="cntryController">
<input list="testList" type="" ng-model="SelectedDoctor" ng-change="LoadSessionData(SelectedDoctor)" />
<datalist id="testList">
<option ng-repeat="x1 in names" value="{{x1.drname}}">
</datalist>
</div>
//here your script
<script>
var app=angular.module('myapp1',[]);
app.controller('cntryController',function ($scope) {
//data for ng-repeat
$scope.names=[{'drname':'Dr.Test1'},{'drname':'Dr.Test2'},{'drname':'Dr.Test3'}]
$scope.LoadSessionData = function(val) {
//console log
console.log(val);
}
});
</script>
This is right way for angular:
<input list="Calle" type="text" ng-model="xCalle" >
<datalist name="Calle" id="Calle" >
<option value="11 DE SEPTIEMBRE DE 1888">
<option value="12 DE OCTUBRE">
</datalist>
Watch type="text" and where the name, id and ng-model are placed.
That will work with angular without any javascript additional function.

HTML dropdown list issues

I'm having issues with the following code:
<form id="form_shower">
<select id="myselect">
<option value="" selected="selected"></option>
<option value="form_name1">Remove the Association</option>
<option value="form_name2">Copy and associate to new form group</option>
<option value="form_name3">Maintain the old association</option>
</select>
</form>
<form name="form_name1" id="form_name1" style="display:none">
Do stuff that removes the association!
</form>
<form name="form_name2" id="form_name2" style="display:none">
New Internal Form Name: <input type="text" name="newinternalformname" value="">
</form>
<form name="form_name3" id="form_name3" style="display:none">
Do stuff to maintain the old association!
</form>
<script>
$("#myselect").on("change", function () {
$("#" + $(this).val()).show().siblings();
})
</script>
Basically, when I select one of the options from the dropdown list, I get what I want (as of now its just test text) but when I select a new option from the list, it appends the data instead of getting rid of what is associated with one of the previously selected options.
It's because the form that contains the dropdown is also a sibling of the form you are showing. Also you have nested forms, which is not allowed
// The first one in this set is always the form with the dropdown
$("#" + $(this).val()).show().siblings()
I would do the following
$("#myselect").on("change", function () {
$(".show-hide").hide();
$("#" + $(this).val()).show();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="form_shower">
<select id="myselect">
<option value="" selected="selected"></option>
<option value="form_name1">Remove the Association</option>
<option value="form_name2">Copy and associate to new form group</option>
<option value="form_name3">Maintain the old association</option>
</select>
</form>
<form class="show-hide" name="form_name1" id="form_name1" style="display:none">
Do stuff that removes the association!
</form>
<form class="show-hide" name="form_name2" id="form_name2" style="display:none">
New Internal Form Name: <input type="text" name="newinternalformname" value="">
</form>
<form class="show-hide" name="form_name3" id="form_name3" style="display:none">
Do stuff to maintain the old association!
</form>

My HTML "Get" method does not show all form inputs

I have a html form, which I need to parse the information on the server side (which is a simple HTTP Java server I have written) so to run a program with those options. Unfortunately when I submit the form, it does not include all the input options, and only shows a subset of them, e.g. /run.html?-z=-w&yse=yse1&tracefile=capacity.1Mbps_c50.txt. There are other form items (such as number boxes fd, rd, per, etc) which are not included. Can someone help me with that? (actually the ones which are not included should be visible, unless the Use a sample tracefile checkbox is checked).
You can see the demo here: http://jsfiddle.net/3at2c/10/
Here is the whole HTML code (remember, if you want to modify the JavaScript, please do not use JQuery; everything should be written in a single file)
<html>
<head>
<script type="text/javascript">
var isTrace = document.getElementById('isTracefile');
var ctTrace = document.getElementById('cttracefile');
var custom = document.getElementById('custom');
isTrace.onchange = function(){
if (!isTrace.checked) {
ctTrace.style.display = "none";
custom.style.display = "block";
} else {
custom.style.display = "none";
ctTrace.style.display = "block";
}
};
</script>
</head>
<body>
<p>
<h3>Please select a configuration below to run the emulator with:</h3>
<form action="run.html" method="GET">
<input type="checkbox" name="-w" value="-w"/>[-w]: wrap around at tracefile end and begin anew at time zero./
<br/>
<input type="checkbox" name="-z" value="-w"/>[-z]: zero packets routed through; route to co-resident webserver
<br/>
<input type="checkbox" name="-b" value="-b"/>[-b]: run the emulator in the background.
<br/>
<br/> YSE Emulator to Run: <select name="yse">
<option value="yse1">YSE1</option>
<option value="yse2">YSE2</option>
<option value="yse3">YSE3</option>
<option value="yse4">YSE4</option>
<option value="yse5">YSE5</option>
<option value="yse6">YSE6</option>
</select> <br/>
<br/>
<input type="checkbox" id="isTracefile" checked />Use a sample tracefile
<!--Wrap the select and its label in a container so that it can be shown/hidden-->
<div id="cttracefile">
Sample Tracefiles to use:
<select name="tracefile">
<option value="capacity.1Mbps_c50.txt">capacity.1Mbps_c50</option>
<option value="capacity.3Mbps_100RTT_PER_0.00001.txt">capacity.3Mbps_100RTT_PER_0.00001</option>
<option value="capacity.3Mbps_100RTT_PER_0.0001.txt">capacity.3Mbps_100RTT_PER_0.0001</option>
<option value="capacity.3Mbps_100RTT_PER_0.001.txt">capacity.3Mbps_100RTT_PER_0.001</option>
<option value="capacity.3Mbps_100RTT_PER_0.01.txt">capacity.3Mbps_100RTT_PER_0.01</option>
<option value="capacity.3Mbps_100RTT_PER_0.05.txt">capacity.3Mbps_100RTT_PER_0.05</option>
<option value="capacity.3Mbps_100RTT_PER_0.1.txt">capacity.3Mbps_100RTT_PER_0.1</option>
<option value="capacity.3Mbps_200RTT_PER_0.0001.txt">capacity.3Mbps_200RTT_PER_0.0001</option>
<option value="capacity.3Mbps_200RTT_PER_0.001.txt">capacity.3Mbps_200RTT_PER_0.001</option>
<option value="capacity.3Mbps_400RTT_PER_0.0001.txt">capacity.3Mbps_400RTT_PER_0.0001</option>
<option value="capacity.3Mbps_400RTT_PER_0.001.txt">capacity.3Mbps_400RTT_PER_0.001</option>
<option value="capacity.13Mbps_200RTT_PER_0.000001.txt">capacity.13Mbps_200RTT_PER_0.000001</option>
</select>
</div>
<!--Wrap the custom inputs and its labels in a container so that it can be shown/hidden-->
<div id="custom">
<label>Forward Delay: </label><input id="fd" type="number" min="0" max="1000" step="1" value ="100"/> ms
<br/>
<label>Reverse Delay: </label><input id="rd" type="number" min="0" max="1000" step="1" value ="100"/> ms
<br/>
<label>Download Capacity: </label><input id="down" type="number" min="1" max="30" step="1" value ="1"/> MBps
<br/>
<label>Upload Capacity: </label><input id="up" type="number" min="1" max="30" step="1" value ="1"/> MBps
<br/>
<label>Packet Error Rate (PER): </label><input id="per" type="number" min="0.00001" max="1" step="0.00001" value ="0.00001"/>
</div>
<br/> <input type="submit" value="Run Emulator!"/>
</form>
</body>
</html>