I can't assign value from hiddenField into loop in JavaScript - javascript

Like in subject I have value getting from server side to hidden field and then I want to loop for this value to shows mine divs, but it didn't work (When I just input var i = 0 it normally works) Here is my code:
var numberOfDivs = parseInt(document.getElementById('<%= numberOfShowedDivs.ClientID %>').value);
var i = numberOfDivs;
function appendText() {
i++;
$("#task" + [i]).show(300);
document.getElementById('<%= numberOfShowedDivs.ClientID %>').value = i;
}
What is wrong?

Related

How to get the CheckBoxlist value using JavaScript

Using below javascript function can able to get the all the text of checkbox list. But can't able to get the value (It passing as "on").
var CHK = document.getElementById("<%=chkFun.ClientID%>");
var checkbox = CHK.getElementsByTagName("input");
var label = CHK.getElementsByTagName("label");
var listOfSpans = chkBox.getElementsByTagName('span');
for (var i = 0; i < checkbox.length; i++) {
var value = checkbox[i].value;
alert("Selected = " + label[i].innerHTML); //Get the text
}
The default value of a checkbox is "on" which is what you are getting. You are not setting the value of the checkbox properly.
If the value attribute was omitted, the default value for the checkbox is on , so the submitted data in that case would be [name]=on.
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/checkbox

create html field based on js variable value

I have a property called results.row in JS. Using the value of that, I have to create textboxes in html. Suppose the value of results.row is 5,then I want to create 5 textboxes in html.
Also, the data to be shown on the textboxes is inside results.rows.item(i) . Is it possible to display?
HTML
<textbox name="Medicine Name" id="medicine"/>
JS
var rows = 5;
for (var i = 0; i < rows; i++)
{
var textbox = document.createElement('input');
textbox.type = 'text';
textbox.value = 'value here ' + i;
document.getElementsByName('medicine')[0].appendChild(textbox);
}

Getting form element by value in Javascript

Hey guys I have a form which is stated below
<td><input type="text" id="NumberofRisks" name="Yeses" style="width: 25px" value ="<?php echo $row['Total-Score'];?> " </td>
The form has some data from a table as a value and I would like to take this value into my Javascript from to do a simple calculation. The function is listed below.
function sum()
{
sumField = document.getElementById("NumberofRisks");
var sum = 0;
$("input[name^='yanswer']:checked").each(function(){
sum++;
});
sumField.value = sum;
}
I need to get that value into the function as the value where it says NumberofRisks. Is there a way I can do this?
Try this to set sum to the initial value of #NumberofRisks.
var prevCheckedCount = 0;
function sum()
{
var sumField = document.getElementById("NumberofRisks");
// start sum with the inital value in HTML from DB
var valueOnClick = Math.floor(sumField.value);
var thisCheckedCount = 0;
$("input[name^='yanswer']:checked").each(function(){
thisCheckedCount++;
});
if(thisCheckedCount <= prevCheckedCount) {
sumField.value = valueOnClick - prevCheckedCount + thisCheckedCount;
} else {
sumField.value =valueOnClick + thisCheckedCount;
}
prevCheckedCount = thisCheckedCount;
}
Here's this code working: http://jsfiddle.net/t5hG4/
In order to grab the value of the input box you can use the following javascript:
var sum = document.getElementById("NumberofRisks").value;
Is this what you're trying to achieve?
As a side note, in the example you provided you did not close the input box so that might be giving you some errors as well.
EDIT:
If you're looking for a pure Javascript way to do this see below:
var elements = document.getElementsByTagName('input');
var sum = document.getElementById("NumberofRisks").value;
for (var i = 0; i < elements.length; i++) {
if (elements[i].checked) {
sum++
}
}
See fiddle - http://jsfiddle.net/es26j/

Populating a hidden field Microsoft JScript runtime error: Object doesn't support this property or method

I am trying to populate a hidden field with the list box items. Currently I have a list box populating with the selected values. I need to somehow populate a hidden field with the values so that I can use them on a different page. So the hidden field value would be like 123456,654987,546845 etc. separated by a comma getting the values from the list box. I am kinda stuck with the code below not sure how to achieve this and I am currently getting the error Microsoft JScript runtime error: Object doesn't support this property or method. Any ideas?
<script language="javascript" type="text/javascript">
function getSelected(source, eventArgs) {
var s = $get("<%=DoctorNameTextBox.ClientID %>").value;
var opt = document.createElement("option");
opt.text = s.substring(s.length - 10);
opt.value = s.substring(s.length - 10);
document.getElementById('<%= NPIListbox.ClientID %>').options.add(opt);
var Source = document.getElementById('<%= NPIListbox.ClientID %>');
var Target = document.getElementById('<%= hidListBox.ClientID %>');
var HiddenList = document.getElementById('<%= hidListBox.ClientID %>') //The hidden field
var SelectedValue = Source.options[Source.options.selectedIndex].value + ','; // Hidden List is comma seperated
var newOption = new Option(); // Create a new instance of ListItem
newOption.text = Source.options[Source.options.selectedIndex].text;
newOption.value = Source.options[Source.options.selectedIndex].value;
Target.options[Target.length] = newOption; //Append the item in Target
Source.remove(Source.options.selectedIndex); //Remove the item from Source
if (HiddenList.value.indexOf(SelectedValue) == -1) {
HiddenList.value += SelectedValue; // Add it to hidden list
}
else {
HiddenList.value = HiddenList.value.replace(SelectedValue, ""); // Remove from Hidden List
}
}
I believe .value is the issue here. Since you are using jQuery, use this: .val()
http://api.jquery.com/val/
I solved it thank you
hid.value = hid.value + opt.text + ',';
Dim data As String = HiddenOptions.Value.TrimEnd(","c)

the best practice for assigning an ID automatically

I have a page that contains a link like this:
new user
When the user clicks the link, I add a new input field to the page. I would like to assign an ID to that input field. I can do this using Javascript:
var num=0;
function add(){
num++;
var input = document.createElement('input');
input.setAttribute('ID',"input"+num);
}
This works; but my question is, is this the standard way, or is there a better way to do this, something that doesn't use num, JS or jQuery?
You don't need an ID on an input element. However you might be needing a name. If so, just use name = "input[]" - then on the server side you will get an array of values.
Since you're using jQuery:
$('<input id="input'+ num +'"/>').appendTo('#yourElement')
Just assign the id to the element:
var num=0;
function add(){
num++;
var input = document.createElement('input');
input.id = "input" + num;
}
Since you tagged your question with jQuery, you can also do this:
function add(){
var input = $('<input id="input' + num++ +'" />')
// do something with the input.
}
Or:
function add(){
var input = $('<input>', {id = "input" + num++});
// do something with the input.
}
Update:
You can't create DOM elements without javascript (jQuery is javascript).
var num=0;
function add(){
num++;
var input = document.createElement('input');
input.id = "input"+num;
}
random ID without num :
function add(){
var input = document.createElement('input');
input.id = 'input'+parseInt(Math.random()*1354243242);
}

Categories