I have some javascript in a .js file that include html code from an external file .htm
After load the html I want to modify the attribute of input text, but I can't do it:
myScript.js
$(".plus").click(function(){
function addRow() {
//I read the current number of rows..
var rowsNumber = $("#rows").val();
//set the progressive number for the new row
rowsNumber++;
var row = rowsNumber;
var row = 1;
//load the html from a file
$.get("defaultHtmlForRow.htm", function(data) {
$("#rowList").after(data);
});
//#descriptionDefault is the id of input type text loaded from defaultHtmlForRow.htm
$("#descriptionDefault").attr('id', 'description'+row ); //i want to rename the id like id="description1"
//#priceDefault is the id of input type text loaded from defaultHtmlForRow.htm
$("#priceDefault").attr('id', 'price'+row ); //i want to reename the id like id="price1"
//the default value for #priceDefault (now, if correct, #price1) is 30.00, this alert will be tell me "30.00" but I see "undefined"
//below, I want to verify that it's all correct
var newPrice = $("#price"+row).val();
alert(newPrice); //tell me "undefined"... like I can't read the attribute and value from defaultHtmlForRow.htm
}
addRow();
});
index.htm
<!-- index.htm -->
<!-- the first code... -->
<input type="hidden" id="rows" name="rows" value="1" />
<div id="rowList">
</div>
<script src="path_to/myScript.js"></script>
<!-- the end code... -->
<!-- end of index.htm -->
defaultHtmlForRow.htm
<!-- the file defaultHtmlForRow.htm -->
<div class="form-group" id="rowDefault">
<div class="col-sm-1 col-lg-1">
<input type="text" class="form-control" id="priceDefault" name="priceDefault" placeholder="Es: 30.00" value="" />
</div>
<div class="col-sm-1 col-lg-1">
<button type="button" class="btn btn-primary plus">
<span class="glyphicon glyphicon-plus" aria-hidden="true"></span>
</button>
</div>
</div>
successcallback function.$.get()is an asynchronous function.id="rowDefault". and also inid="priceDefault"