Value of selected list - javascript

I have a select list with dynamic content such
Honorar, 120.00
Porti, 7.50
Spesen, 12.00
This values are stored x_ko_leistungsart. I am selecting only one option (no multi selection) and would like to have the values after comma such 120.00 for Honorar and 7.50 if the the option porti is selected.
I am using the following function to get these values. unfortunately it does not work. I get only the value 10 assiged...
Could you please have a look to the code where the mistake can be?
Regards
thanks
mpol_ch
function SelectAnsatz() {
document.fkostenedit.x_ko_ansatz.value = '10';
var Ansatz=0;
var splitted;
var elements = document.getElementsByName("x_ko_leistungsart[]");
splitted = elements.nextSibling.nodeValue.split(",");
Ansatz = parseFloat(splitted[1]);
document.fkostenedit.x_ko_ansatz.value = Ansatz.toFixed(2);
}

Take a look at document.getElementsByName("x_ko_leistungsart[]");
I'll give you a hint: The "value" you're getting from that is not what you're expecting. You can reduce at least two lines of code by expanding on the getElementByName function.
You can find more information here: http://www.w3schools.com/dom/dom_nodes_get.asp

I solves my problem with an onchange even on x_ko_leistungsart. Here is the function:
Thanks
mpol_ch
function SelectAnsatz(){
var x=document.getElementById("x_ko_leistungsart");Ansatz=x.options[x.selectedIndex].text.split(",");
document.fkostenedit.x_ko_ansatz.value = Ansatz[1];
}

Related

Adding "values" to a texfield in jquery/js

When writing a new email, I've got a modal(pop-up window in boostrap) that shows a list of contacts. When I select (through checkboxes) a couple of contacts, the selected ones are written into a checkbox. Problem is I'm just writing the lastone I select instead of all of the selected ones.
If you need further explanation please ask. (Sorry for my english)
$("#tblContacto").on("click", ".ck", function(event){
if($(".ck").is(':checked')) {
selected_index = parseInt($(this).attr("alt").replace("Check", ""));
var contacto = JSON.parse(tbContactos[selected_index]);
$("#txtDestinatarios").val(contacto.Email);
} else {
$("#txtDestinatarios").val("");
}
});
Assuming that you want to add all E-Mails into a textfield with id txtDestinatariosthe cause of your Problem is the usage of the $("#txtDestinatarios").val(); function.
Calling val() with an argument sets (and thus overwrites) the value within the textfield. (See demo at http://api.jquery.com/val/#val2)
You would have to first retrieve the value of the textfield using code like var currentValue = $("#txtDestinatarios").val() and then add/remove the E-Mail from/to the string before setting the resulting string back as the value.
If you want to set all selected items in the checkboxes into Textfiled you can use the following line of code :-
$("#txtDestinatarios").val( $("#txtDestinatarios").val()+ ","+contacto.Email);

Trying to use Jquery/javascript to calculate form fields

Im having a terrible time trying to calculate the value of two form fields. I'm constantly getting "NAN" which would indicate non-numeric input, this is despite the fact that the form fields are only populated with numbers.
In response I tried to use ParseInt to get a numeric value. This also fails to yield a successful result.
This is what I have so far. Any help is appreciated.
$('#value-calc input').change(function () {
var valueINT = parseInt($('#value'),10);
var quantINT = parseInt($('#quantity'), 10);
var math = ((valueINT/quantINT)*1000);
$('#cpm').val(math);
});
http://jsfiddle.net/greyoxide/YRWAA/1/
You need to add the .val() method after your selectors to get the value they hold
working fiddle
You're not passing in strings when you refer to elements by ID with that jQuery.
Perhaps try $('#value').val() in the parseInt methods instead.
Try this:
$('#value-calc input').keyup(function () {
var valueINT = $('#value').val();
var quantINT = $('#quantity').val();
var math = ((valueINT/quantINT)*1000);
$('#cpm').val(math);
});

Cloned row requesting same function [duplicate]

This question already exists:
Closed 10 years ago.
Possible Duplicate:
Call same function by a cloned list row
I am trying to make a simple calculation to work.
I have the following running:
http://jsfiddle.net/vSyK6/41/
Basically, the way it works now is this:
When you select an option on the drop down list it will display the content based on the option selected. Then when you select the same option again it will add, basically clone the same row.
Now, when the second option is selected "Option2" it will display an empty textbox. When you enter a number it will or should call the a function where we make a basic calculation. The function is already in the script.
However, when we have two empty textboxes it should call the same calculation function but calculate seperately and puts it in a different div. The div# where we display the amount is a called "amount"
Basically, it should work like this:
First Empty textbox -> 100 -> 100 * 22.38 = display result in div#1
Second Empty textbox -> 230 -> 230 * 22.38 = display in div#2
any idea on how to accomplish that ?
When cloning elements the id is cloned as well. It is best practice to create a new ID for the cloned elements, which will also help in accomplishing what you want. The same goes for the name attribute as well.
With a few modification to your code, http://jsfiddle.net/dNQVQ/3/, I was able to get what you were after. Let me first say that this might not be the ideal way to go, but it is a start. Like I said earlier the key is going to be setting unique ids for the cloned elements. What I did in this example was use a index as part of the list element id that is cloned with a matching index in an 'amount' div. This way when an input is updated the index is retrieved and then used to update the appropriate div. Additionally, I moved the function that did the calculation and updates to an anonymous function in the settimeout call. This makes it easy to use a reference to the updated input in the function call.
Joining the party quite late here :) Here is one vernon: http://jsfiddle.net/KVPwm/
ALso if its assignment bruv, put an assignment homework tag!
People around SO community are awesome folks so be truthful, guys will help man!
Use .on instead of live - recommendation. i.e. upgrade your JQ source if keen read this - What's wrong with the jQuery live method?
you have 2 document.ready functions also I chained few things for you.
Also think of using isNan check as well.
Rest you can read the code and play around a bit to make it more concise.
I have added 2 divs and using the id number to populate the stuff accordingly.
This should fit the cause :)
code
$("document").ready(function() {
/////////////////////////////////CALUCATIONS/////////////////////////////////
//setup before functions
var typingTimer; //timer identifier
var doneTypingInterval = 0; //time in ms, 5 second for example
$('input[name=Input2], input[name=Input1]').live('keyup', function() {
var str = $(this).prop("id");
var pattern = /[0-9]+/g;
var matches = str.match(pattern);
amount = parseFloat($(this).val()) * 22.38;
typingTimer = setTimeout(doneTyping(matches), doneTypingInterval);
});
$('#Input2').keydown(function() {
clearTimeout(typingTimer);
});
function doneTyping(matches) {
$('#amount'+matches).text(amount.toFixed(2) + " lbs");
}
$("#List-Option1,#List-Option2").hide();
$('#category').change(function() {
var str = $('#category').val();
if (str == 'Option1') {
var option1 = $("#List-Option1:first").clone().show();
$('#box li:last').after(option1);
}
if (str == 'Option2') {
var option2 = $("#List-Option2:first").clone().show();
$('#box li:last').after(option2);
}
});
});​

problem with dynamicallly adding values to dropdown box

i want to dynamically add options to drop down boxes
var x =document.getElementById("c");
var optn = document.createElement("OPTION");
optn.text="hhh"
optn.value="val"
x.options.add(optn);
I am doing it inside a loop,with diff values of for val and hhh.Bur sometime i dont see any any values in drop down box , what may be the problem?
Try this one:
var objSelect = document.getElementById("subComponentOSID");
objSelect.options[objSelect.options.length] = new Option('1','1');
objSelect.options[objSelect.options.length] = new Option('2','2');
add is a method of HTMLSelectElement objects, not of HTMLCollection objects.
x.add(optn)
Assuming the element with the id "subComponentOSID", the only apparent issues in your javascript are missing semicolons on the lines where you assign values to optn.text and optn.value. Also, while most browsers will resolve what you mean when calling the add function on an options collection for a select element, you should move your add to the select itself. See the Mozilla reference for HTMLSelectElement, which provides an example.
In the meantime, try replacing the code snippet you provided with this:
var x =document.getElementById("subComponentOSID");
var optn = document.createElement("OPTION");
optn.text="hhh"; //Added semi-colon
optn.value="val"; //Added semi-colon
x.add(optn); // Moved add to HTMLSelectElement

How to do this in jQuery

I have the following code:
var tempIdx = document.getElementById('cityBuild').selectedIndex;
var tempBuilding = document.getElementById('cityBuild').options[tempIdx].value;
// Do stuff with tempBuilding
I spent some time trying to do it in jQuery under the assumption it'd be easier, it wasn't. I tried what was given at this site but is never returned any of the values. Am I doing something wrong or is this something that jQuery simply doesn't do?
The item is a select box (single selection only) and I want to get the value from it.
jQuery knows how to work with select elements. You can get the value as any other input:
var value = $('#cityBuild').val();
Little variation to Eran answer:
var value = $('#cityBuild option:selected').val();
EDIT: now Eran is in the right. :)

Categories

Resources