I have repeating data that gets injected into the html like this:
<div class="subFormContent" id="Results">
<div class="subFormContentRow">
<div class="subFormContentRowChildWrapper">
<div id="subCtrlPreviewRow1-identifier" class="subCtrlPreviewRowContainer">
<div id="subCtrlRow1column1-identifier" class="subCtrlPreviewCell">
<div class="subCtrlPreviewCtrlHolder">
<div class="control ">
<label class="block label alignLabel">
<span>value</span>
</label>
<input type="text" class="textInput block field" id="value-identifier" value="value1" />
</div>
</div>
</div>
<div id="subCtrlRow1column2-identifier" class="subCtrlPreviewCell">
<div class="subCtrlPreviewCtrlHolder">
<div class="control">
<label class="block label alignLabel">
<span>propName</span>
</label>
<input type="text" class="textInput block field" id="propName-identifier" value="propname1" />
</div>
</div>
</div>
</div>
</div>
</div>
</div>
...Repeats
I need to get the value from column 1 where column 2 is equal to x with JavaScript or jquery, but it will not always be on the same row number. It will however only have one row where column 2 is x. Can anyone think of a way to do this?
updated for multiple rows
1.2 update if id's starts with words "value" and "propName"
$(document).ready(function(){
var x = 5,
row = $('.subFormContentRow');
row.each(function(index, el){
var inputProp = $(el).find('[id^="propName"]'),
inputVal = $(el).find('[id^="value"]');
if(inputProp.val() == x){
alert(inputVal.val());
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="subFormContent" id="Results">
<div class="subFormContentRow">
<div class="subFormContentRowChildWrapper">
<div id="subCtrlPreviewRow1-identifier" class="subCtrlPreviewRowContainer">
<div id="subCtrlRow1column1-identifier" class="subCtrlPreviewCell">
<div class="subCtrlPreviewCtrlHolder">
<div class="control">
<label class="block label alignLabel">
<span>value</span>
</label>
<input type="text" class="textInput block field" id="value-456456456" value="value1" />
</div>
</div>
</div>
<div id="subCtrlRow1column2-identifier" class="subCtrlPreviewCell">
<div class="subCtrlPreviewCtrlHolder">
<div class="control">
<label class="block label alignLabel">
<span>propName</span>
</label>
<input type="text" class="textInput block field" id="propName-45647898778645656" value="5" />
</div>
</div>
</div>
</div>
Shouldn't be using ids in a way that they show up multiple times on the page.
You can still do it by finding the element, traversing back up to the row, then finding the value.
With JQuery:
$("#propName[value='" + x + "']")
.closest(".subFormContentRow").find("#value").val();
Related
I have a form with a input was type="number", after submit I made it reset and backup in a list to resolve purpose.
If that any submit is failed, I want to resolve entire input. I also try convert innerHTML to Number before assign, but it not working. Number.parseInt(document.querySelector("div#"+ item_Id +".result #context > #cmeasurement").innerHTML);
I have that possible by change a number input's value?
function resolveForm(item_Id) {
document.querySelector("#addInventoryform #measurement").value = document.querySelector("div#" + item_Id + ".result #context > #cmeasurement").innerHTML;
}
<div id="addInventory_form">
<form id="addInventoryform" autocomplete="off">
<label for="measurment">Measurement</label>
<input id="measurment" name="measurement" type="number" value="1">
</form>
</div>
<div id="a123121" class="result" style="background-color: wheat">
<div>
<button onclick="resolveForm('a123121')" id="result_id">123121</button>
<div id="status"></div>
</div>
<div id="context" style="display:none">
<div id="cmeasurement">12312</div>
</div>
</div>
On the input element in your HTML, you set your id="measurement".
In your function, you're calling your id as "measurment". If you fix your spelling, the function should work as expected.
See the snippet below:
function resolveForm(item_Id) {
document.querySelector("#addInventoryform #measurement").value = document.querySelector("div#" + item_Id + ".result #context > #cmeasurement").innerHTML;
}
<div id="addInventory_form">
<form id="addInventoryform" autocomplete="off">
<label for="measurment">Measurement</label>
<input id="measurement" name="measurement" type="number" value="1">
</form>
</div>
<div id="a123121" class="result" style="background-color: wheat">
<div>
<button onclick="resolveForm('a123121')" id="result_id">123121</button>
<div id="status"></div>
</div>
<div id="context" style="display:none">
<div id="cmeasurement">12312</div>
</div>
</div>
I have this part of form field
When user click on the Add button will copy the above form field and user can add more in this field .
but when I add <div id="ownership"> above the code form it start to look like this
I need to put id=ownership because I want to use it in my jQuery function
<div id="ownership">
<div class="col-lg-12 mb-30 ">
<label for="add_owner"><b><span style="color:#e60000;">*</span> Name</b></label><br>
<input class="from-control" type="text" id="name" >
</div>
<div class="col-lg-6 mb-30" >
<label for="add_owner"><b><span style="color:#e60000;">*</span> Phone number</b></label><br>
<div class="input-group-prepend">
<input class="from-control" type="text" id="phone" >
</div>
</div>
<div class="col-lg-6 mb-30" >
<label for="add_owner"><b><span style="color:#e60000;">*</span> Emailn</b></label><br>
<div class="input-group-prepend">
<input class="from-control" type="email" id="email">
</div>
</div>
</div>
<div id="owner"></div>
<br>
<div class="col-md-12">
<button type="button" class="btn btn-success w-30 add_info f-r">Add</button>
</div>
<script>
$(document).ready(function(){
$(".add_info").click(function(){
var newForm = $("#ownership").clone();
$('input', newForm).val('');
$('owner').append(newForm);
});
});
</script>
How do I fix the code in order to achieve the image in number 1 ?
try this
<div class="row">
<div class="col-sm-6"> Text</div>
<div class="col-sm-6"> Text</div>
</div>
you need to apply this format then design will be properly according your desirable.
Try this way. Always use bootstrap col class inside row class then it will not cause unexpected result.
<div class="row">
<div class="col-md-12"></div>
<div class="col-md-6"></div>
<div class="col-md-6"></div>
</div>
I want to get the child element dynamically. I am looping through the div tag to get the values. But its not working.
<div class="pg">
<div class="row">
<div class="col-3">
<div class="fg">
<input type="text" xl="12" value="a">
</div>
<div class="fg">
<input type="text" xl="34" value="b">
</div>
</div>
</div>
</div>
The javascript function for fetching the value dynamically
This code is working if row and col div are not there. But when they are included its not working at all. Anyone can please help me with this issue!
$(".pg").each(function(){
$(this).children(".fg").each(function() {
console.log($(this).attr('xl'));
});
});
First using children(), will return only direct children , not deep search ,
Second , use data-xl instead of xl ,more information here about data attribute
also , the loop is accessing the parent div of input , so use ".fg input" instead
for searching use find() , then change selector to access directly your input , .find(".fg input")
See below working snippet :
$(".pg").each(function() {
$(this).find(".fg input").each(function() {
console.log($(this).val() ,"=", $(this).data('xl'));
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="pg">
<div class="row">
<div class="col-3">
<div class="fg">
<input type="text" data-xl="12" value="a">
</div>
<div class="fg">
<input type="text" data-xl="34" value="b">
</div>
</div>
</div>
</div>
<div class="pg">
<div class="row">
<div class="col-3">
<div class="fg">
<input type="text" data-xl="15" value="c">
</div>
<div class="fg">
<input type="text" data-xl="84" value="d">
</div>
</div>
</div>
</div>
I am trying to get the next closest input of same class or different class that is available in the next row div child it says undefined am unable to get it.
[Fiddle]
$(".std_amt").change(function() {
alert($(this).parent('.row').next(".row").children("input.std_amt").val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="row">
<div class="col-lg-3">
<div class="form-group ">
<input class="form-control std_amt" type="text" name="relative_name_0" id="relative_name_0" value="">
<label class="help-inline"></label>
</div>
</div>
</div>
<div class="row">
<div class="col-lg-3">
<div class="form-group ">
<input class="form-control std_amt" type="text" name="relative_name_1" id="relative_name_1" value="">
<label class="help-inline"></label>
</div>
</div>
</div>
Try to use closest instead of parent like below
$(".std_amt").change(function() {
alert($(this).closest('.row').next(".row").find("input.std_amt").val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="row">
<div class="col-lg-3">
<div class="form-group ">
<input class="form-control std_amt" type="text" name="relative_name_0" id="relative_name_0" value="">
<label class="help-inline"></label>
</div>
</div>
</div>
<div class="row">
<div class="col-lg-3">
<div class="form-group ">
<input class="form-control std_amt" type="text" name="relative_name_1" id="relative_name_1" value="">
<label class="help-inline"></label>
</div>
</div>
</div>
.parent('.row') looks at the direct parent, it does NOT climb the tree. You need to use closest('.row') to reference the row. And you should use find() and not children() since the input is not a direct child.
$(this).closest('.row').next(".row").find("input.std_amt")
I've been working on this for a little longer then needed, but I can't find what is going wrong here. I think I've looked at it way too long. I need to get the items to add up after quantity is add to the form.
Here is my HTML
<div class="row-fluid container-cart">
<div class="span4">
<div class="thumbnail">
<img src="http://myurl.com/image.jpg" />
<div class="caption">
<h3 class="">Title</h3>
<p class="">Description</p>
<div class="row-fluid">
<div class="span6">
<p class="lead">
<span id="item-price">100</span>
<span class="price-integer">
<input type="hidden" value="100">
</span>
</p>
</div>
<div class="span6">
<span>Quantity</span>
<input type="text" name="" id="quantity" class="stored quantity" autocomplete="off" value="">
</div>
<div class="span6">
<input type="text" name="base-total" id="base-total" class="stored readonly base-total" value="" readonly>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="span6">
Total:
<span class="total-cart">
<input type="text" name="total-cart" id="total-cart" class="stored readonly" value="" disabled="disabled">
</span>
</div>
Here is my JS
$('.quantity').on('keyup', function() {
var sum = 0;
$(".container-cart").each(function(i,o){
total = parseInt($(o).find(".quantity input").val(), 10) * parseInt($(o).find(".price-integer input").val(), 10);
if(!isNaN(total) /*&& total.length!=0**/) {
$(o).find(".base-total").val(total);
sum += total;
}
});
$("#total-cart").val(sum);
});
Looks like you typed a wrong selector, it's the .quantity input, the .quantity is an input, so it won't have any children, maybe you want to mean the input.quantity which means finding the input having class quantity.
Demo.