How to retrieve value from dynamically created textbox by javascript using Vb - javascript

I am using javascript code to add dynamic text box and it is work.
But there are problem when I wish to get the value that enter in the dynamic created textbox in Vb.
I tried to put runat="server" in the DIV that I use to add textbox, but the add function cannot work after I put runat="server" in the DIV.
Anyone know how to retrieve value from the textbox that are dynamically added by javascript by using Vb?
Below is my javascript code use to add textbox.
var x = InputsWrapper.length;
var FieldCount = 1;
$(AddButton).click(function(e) //on add input button click
{
if (x < MaxInputs) {
FieldCount++;
var setID = ("field_1" + FieldCount);
$(InputsWrapper).append('<div><input id="' + setID + '" type="text" placeholder="Item title..."/>×</div>');
x++;
}
return false;
});
<

I modify the textbox by adding name="txtDy" and using Request.Form in Vb.net to retrieve the value.
Vb will automatic save the value of all "txtDy" textbox and split it by default symbol, something like ("123,abc,456,def").
So I split the string and using for loop to get the value of each textbox.
Dim txtDyList As String = Request.Form("txtDy")
Dim txtDynamic() As String = txtDyList.Split(",")
For i As Integer = 0 To txtDynamic.Length - 1
MsgBox(txtDynamic(i)) 'I can get the value from here: txtDynamic(i)
Next

Related

inserting random number into hidden field on submit

Trying to insert a random number into a hidden field in a form using javascript. Not passing any value since SQL error of - doesn't have a default value
var randomNum = '';
randomNum += Math.round(Math.random()*5);
var elem = document.getElementById("randSS").value = randomNum;
<input name="randSS" type="text" id="randSS"/>
Didn't have protected fillable added!

Preserving quotes in jquery string

I'm creating options of a dropdown using jquery. User will enter the option text in the textbox keyvalue and on keyup() it will be added to <select>.
Eg: If user enter string Size , an option will be added in the dropdown like
<option value="Size">Size</option>
HTML
<input type="text" id="keyvalue" onkeyup="addvaluetoselect()">
<select id="key"></select>
jQuery
var keyvalue = $('#keyvalue').val();
if ($("#key option[value='"+keyvalue +"']").length == 0)
$('#key').append('<option value="'+keyvalue +'">'+keyvalue +'</option>');
If user types a value including double or single quotes, it should be added to the option. But currently it is not working. I tried by adding
var keyvalue = keyvalue .replace(/(['"])/g, "\\$1");
to escape the quotes. But when I try to enter a option Size's , the option is getting created like Size/'s instead of Size's.
Can anyone help me to fix this. Thanks in advance.
Don't append this as raw string, build it properly instead:
var keyvalue = $('#keyvalue').val();
var exists = $('#key option').map(function() {
return $(this).val();
}).toArray().indexOf(keyvalue) >= 0;
if (!exists) {
var newOption = $("<option></option>");
newOption.attr("value", keyvalue);
newOption.text(keyvalue);
$('#key').append(newOption);
}
As you can see, the check to see if the value already exists was also wrong, fixed that by iterating the existing options and checking their value against the value typed by the user.

How to get value of textbox created on the client side in the code behind?

I am creating dynamic textboxes using jquery. I want to access each textbox value in the code behind using asp.net.
HTML (.aspx)
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$("#btnAdd").bind("click", function () {
var div = $("<div />");
div.html(GetDynamicTextBox(""));
$("#TextBoxContainer").append(div);
});
function GetDynamicTextBox() {
return '<input id="dynamictxtbx" name = "DynamicTextBox" type="text" /> '
}
</script>
Codebehind (.aspx.cs)
protected void ListView1_ItemUpdating(object sender, ListViewUpdateEventArgs e)
{
// I want to access values here //
}
I want to access the values inside the ListView1_ItemUpdating function. How can I do this?
First of all You need to provide unique id to the dynamic control You're adding because html won't allow you to add any element with duplicate id. Also You can get the text value, ensure that you have set the name property of textbox like given below while dynamically adding it.
'<input id="dynamictxtbxuniqueID" name = "DynamicTextBox" type="text" />'
Now in order to get it into the code behind use the below mentioned C# code to access the dynamic textbox
//Get the Textbox value
string textboxval = Request.Form["DynamicTextBox"];
Use this link for further reference

Jquery/Javascript get id of a textbox using the id of another textbox

I want to use a jQuery/JavaScript function to validate the input of a textbox.
I have pairs of checkboxes like this:
<input type="text" id="MinMondayTxt" />
<input type="text" id="MondayTxt" />
this is for each day of the week (MinTuesdatTxt, MinWednesdayTxt...)
The Function is like this:
function ValidateTxt(TxtId){
}
It take the Id of a textbox as input. What I want to do is the following:
1) Check if the Id starts with "Min"
2) Get the corresponding textbox, by removing "Min" from the id. for example if we have MinMondayTxt I want to remove the "Min" in order to access the MondayTxt textbox
3) Compare the the values of the 2 textboxes (all values are numeric)
4) if MinMondayTxt > MondayTxt Alert the user
I am looking for help for step 1) and 2) : How can I check if the id starts with "Min" and how to get the corresponding textbox id (by removing the min)
You can use replace() method with the #id selector of jquery as below
function ValidateTxt(TxtId){
var withoutminid = TxtId.replace(/^Min/, '');
var withouminidval = $('#' + withoutminid).val();
var minidval = $('#' + TxtId).val();
}
This will remove "Min" if present, then fetch the jQuery object:
$('#' + TxtId.replace(/^Min/, ''))
maybe you could try with split:
txtId.split('Min');
if Min exist it should create an array like that :
array = ["Min","MondayTxt"];
then you just have to use array[1].

Is it possible to collect data on checked or unchecked tick boxes in HTML?

I'm looking at creating a very simple HTML page that contains an HTML form with a series of check boxes and input fields. The page would be roughly like so:
Input Forename ==== Input Surname ==== Input ID =====
Text checkbox1 value1 Text
Text checkbox2 value2 Text
Text checkbox3 value3 Text
Text checkbox4 value4 Text
Output Text value
Output Text value
Output Text value
Output Text value
Depending on which values are ticked (i.e. value 1/2/3/4), the value next to it is then calculated (for arguments sake let's say it's multiplied by 2) and then output below (where it says "Output text" value). Apologies if this doesn't match the "question" but you see what I mean by "collecting" data (i.e. the value next to the checkbox).
My question is thus, is it possible to use JavaScript to say, if tickbox1 is checked, please use "value1"? Once this is all completed the page just needs to have a "print" button (which is fine). There is no back end stuff that needs to happen and no data capture.
Hope this makes sense and thanks in advance
EDIT:Of course if it's a lot easier to do this with something like JQuery I'd be happy to explore this!
This code should help you out!
Questions[] is an array with all the input fields in,
getElementsByName goes through all the elements that have the same name (radio boxs for example all have the same name) and then finds out which one is checked, and then uses that value.
Answers[] is the array with the stored values in.
for (var i = 0; i < questions.length; i++) {
//Does the question name have more than one instance? (That would make it a checkbox or radio button...
if (document.getElementsByName(questions[i]).length > 1) {
//Lets loop through all these values and find out which one is selected, and then grab the value from it.
var x = document.getElementsByName(questions[i])
for(var k=0;k<x.length;k++)
if(x[k].checked){ //This is where it loops to find the checked answer
answers[i] = x[k].value;
//alert("radio saved " + answers[i]);
}
}
//Nope, only once, so its a text field or similar
else {
answers[i] = document.getElementById("answer" + questions[i]).value;
//alert("text saved " + answers[i]);
}
}
Hope this helps!
So to use this in your calculations (to say make it double) you could call
var answertoyourquestion = answers[i] + answers[i];
Well, you could try something like this(not tested):
var total = 0;
for (int i = 0;i < 10;i++)
{
var checkbox = document.getElementById("checkbox" + String.ValueOf(i));
if (checkbox.checked)
{
var valuebox = document.getElementById("value" + String.ValueOf(i));
total += (valuebox.value * 2);
}
}

Categories