Hi I have a datatables based table that holds data. I need to be able to get the values out of the following string that I have been able to do so far.
<input name="jobNo" value="job_no_123" />job_no_123
I need to be able to get the name and the value and store into separate variables that I then pass off to do something else.
However I do also have another 4 fields on the page that I need to capture too:
<input name="item_1" value="data1" />data1
<input name="item_2" value="data2" />data2
<input name="item_3" value="data3" />data3
<input name="item_4" value="data4" />data4
And of top of this, this would only be the data from one row, and I need to do this for multiple rows too. But I need to start somewhere.
Please help.
Thanks
I am not sure what are your needs but here is a code that should help
$('input').each(function(i, val){
$(this).attr('name'); // will get the attribute name
$(this).attr('value'); // will get the attribute value
$(this).attr('text'); // will get the text inside the input
});
This regex can find the values from your code. However, it assumes that the input is strictly in that format, with no variations in even whitespace.
<input name="(.*)" value="(.*)" />
The values will be in captured groups 1 and 2.
Note: I am aware that regex should not be used to parse HTML, but if the input is strict enough, it works.
Related
Im working on a project that uses jquery repeater. I need to loop through the inputs elements to make my validations. So, here is an example of an input of my form:
<input name="group-fundos[0][fundo-nome]" placeholder="Nome do Fundo" type="text">
Ans you can see, its like an array. So I need to get all these elements via jquery. How do I do that?
Edit: One other detail. Im using multi-step form. So for each step, I need to loop through its own inputs. The inputs types can be varied: option selects, textfields, textareas, etc..
You could try to retrieve all values by using jQuery [serializeArray] method. For example.
// assuming that your input fields are inside a certain form
var oFormData = $('#multi-step-form').serializeArray();
for (var iIndex in oFormData) {
console.log(oFormData[iIndex].value);
}
Just make sure all your [input fields] have the [name] attributes as well.
Hope this helps for your case
if you are certain values begin at 0 and end at length, you could try something like:
var i = 0;
while($("input[name='group-fundos["+i+"][fundo-nome]']").length) {
//validate here or build a js array for later
}
Edit
Using attribute contains selector you could get elements like the one provided.
$("input[name*='group-fundos["+i+"]") would get all elements with name beginning in "group-fundos[ i ]". (Notice the *=).
I am trying to come up with a solution for converting dot to comma in an input field with type="number", using either JS or JQuery.
The problem is two-fold:
1) It needs to replace the dot no matter where it is located in the input field. Values can be for example:
0.50
.45
12.45
12.
2) I can't use keyup as the trigger. This field is being populated by an externally connected scale, which sends the number, including the dot, in one go.
I hope someone can help with this, it's really doing my head in.
** UPDATE **
I managed to get this piece of code to work, but only if the input is TEXT. If I use NUMBER it just wipes the input clean (and I need it to be NUMBER in order to perform other on-page calculations with the number).
document.getElementById('purchaseQuantity').addEventListener('input', function() {
document.getElementById('purchaseQuantity').value = this.value.replace('.', ',')
});
Thanks,
Use a hidden field with name and update the value within the handler.
document.getElementById('number').addEventListener('input', function() {
document.getElementById('hidden').value = this.value.replace('.', ',')
})
<input type="number" id="number" />
<input type="hidden" name="number" id="hidden" />
var numComma = (value.toString()).replace(".", ",")
Convert the number to string and then use .replace(".",",")
Im new in Angular and I stared working in one project witch is using it for front end side. The thing is. I have a dropdown witch has values 1,2,3 etc... and when you select something in dropdown, depending on what you click. currentEntry variable is changing value and im filtering data value="{{entry['properties'][currentEntry]['password']}}". This works perfectly when I have simple input tag.
But when I do this:
<input type="password" name="pasword" ng-model="password" ng-change="addProperty(password,'password')" class="form-control" placeholder="Password" value="{{entry['properties'][currentEntry]['password']}}">
whats happening up there is that ,value properity is changing when i do inspect element in code but not on the client.
Than i realized that value is stored in ng-model, so I need to somehow create this models dinamicaly for example when I click on drop down item witch has value 1 ng-model should look like this ng-model="password1" etc...
I have this number stored in variable "currentEntry" so i tried to do something like this
<input type="password" name="pasword" ng-model="password{{currentEntry}}"......>
but I get syntax error
Token '{' is an unexpected token at column 9 of the expression [password{{currentEntry}}] starting at [{{currentEntry}}].
How to solve this?
You can't do dynamic variables like that in Javascript (this is not php). Instead use a function or an object.
Function
$scope.password = function(){
//use $scope.currentEntry here
return value;
}
and in your view
<input type="text" ng-model="password()"/>
Object
Another possibility would be this
<input type="text" ng-model="passwords[currentEntry]">
Where $scope.passwords is an object containing your passwords as such.
$scope.password= {1: 'a password', 2: 'another password'}
Also see Use dynamic variable names in JavaScript for more on dynamic variables in Javascript.
I searched, but couldn't find anything that really corresponds to what I'm trying to do.
So I have a text input and next to it and number input. What I want to do is modify the text input's value when the number input has been changed. But I don't want to replace the value entirely.
This is essentially what I currently have:
<input id='field_4' name='4' value='64_to_74_weight_3'/>
<input id='field_4-weight' type='number' min='1' max='5' value='3' data-for='#field_4' />
What I'm trying to do is only change the number after weight_ to correspond to the number being chosen by the number input. I know I can get the last character by using slice(-1), but I can't get it to change it while preserving the rest of the text input's value.
Can anybody help with this? Thank you
Edit:
I had used something similar to below and thought it would've worked, but figured jQuery would be better, but I couldn't get it working on jsFiddle.
function updateInput(weight){
document.getElementById('field_" . $filter->id . "').value.slice(-1) = weight;
}
$("#field_4-weight").on('change', function() {
var txtField = $("#field_4"),
txt = txtField.val();
txtField.val(txt.slice(0, txt.length-1) + $(this).val());
});
Using jQuery, I change the value of an input text field through some process. After the process is done, I need to capture this change and apply it to another process. My problem is that I can't seem to capture this change and assign it to a variable. I know the changes are happening because the DOM is getting updated. Furthermore, this variable assignment works in IE, but not for the other browsers I tested.
Below is a snippet to prove my point (and you can see this online here: http://jsfiddle.net/xMwAE/).
<form>
<input type="hidden" name="my_hidden" value="Hidden Field" />
<input type="text" name="my_text" value="Text Field" />
</form>
$().ready(function() {
$('input[name=my_hidden]').val('Hello Hidden Field');
$('input[name=my_text]').val('Hello Text Field');
// Display
var temp = $('form').html();
// Though the DOM is updated with the new values. The variable temp
// does not capture the changes to the input text field, but captures
// the change in the hidden field. When in IE, temp captures the
// changes in both fields.
alert(temp);
});
Obviously, I need consistent behavior across browsers. Any ideas what's going on?
I don't get any trusted idea what happens, but somehow there should be a difference between setting the value as a member (input.value) or setting the value as a attribute-node.
This works for me :
$('input[name=my_text]').each(function()
{ this.setAttribute('value','Hello Text Field');});
I guess its a bug in innerHTML, see bugzilla: https://bugzilla.mozilla.org/show_bug.cgi?id=535992
Alternatively, you can store the values of your fields into array and use however you like like this:
var data = [];
$('form :input').each(function(){
data.push(this.value);
});
Now you can check for values like this:
alert(data[0]);
alert(data[1]);