dynamic - JavaScript, get dynamically created label's id -
the label beings created holds guid need later. need grab information after list of labels created. here's code:
<button onclick="getalllistings()">get listings information</button> <br /> <div id="divdatainsert" name="divdatainsert"> @foreach (mvctest1.models.listing foundlistings in model._listings) { string pk_listing_id = "listingsid_" + foundlistings.pk_listings_id; string addresspk = "address_" + foundlistings.pk_listings_id; string address = foundlistings.address.tostring(); string citypk = "city_" + foundlistings.pk_listings_id; string city = foundlistings.city.tostring(); string statepk = "state_" + foundlistings.pk_listings_id; string state = foundlistings.state.tostring(); string zipcodepk = "zipcode_" + foundlistings.pk_listings_id; string zipcode = foundlistings.zipcode.tostring(); string fulladdress = address + ", " + city + " " + state; if (foundlistings.pk_listings_id != null) { <input type="text" id="lbllisting_@pk_listing_id" value="@pk_listing_id" /> } } </div> function getalllistings(){ //var listingarray = [document.getelementbyid("lbllisting_")]; (var = 0; < [document.getelementbyid("lbllisting_")].length; i++) { var listingstring = document.getelementbyid("lbllisting_").value; var guid = listingstring.split("_"); alert(guid[1]); i++; } }
my code behind
public actionresult index() { string sql = "select top 10 [pk_listings_id], [address], [city], [state], [zipcode] dbo.listings"; listingcollection listoflistings = new listingcollection(); listoflistings._listings = new list<listing>(); using (sqlconnection conn = new sqlconnection(webconfigurationmanager.connectionstrings["mvcinsertdata"].tostring())) { conn.open(); using (sqlcommand cmd = new sqlcommand()) { cmd.connection = conn; cmd.commandtype = system.data.commandtype.text; cmd.commandtext = sql; using(sqldatareader reader = cmd.executereader()) { if (reader != null) { while (reader.read()) { listing listing = new listing(); listing.pk_listings_id = convert.toint32(reader["pk_listings_id"]); listing.address = reader["address"].tostring(); listing.city = reader["city"].tostring(); listing.state = reader["state"].tostring(); listing.zipcode = reader["zipcode"].tostring(); listoflistings._listings.add(listing); } } } } conn.close(); } return view(listoflistings); }
one of answers involved adding js array in code behind. how do that?
*****update*****
i have changed input this:
<input type="text" class="lbllisting_" value="@pk_listing_id" />
and have adjusted js this:
function getalllistings(){ var listingsarray = document.getelementsbyclassname("lbllisting_"); (var = 0; < listingsarray.length; i++) { var listingstring = listingsarray.value; var guid = listingstring.split("_"); alert(guid[1]); } }
keep in mind, js not inside document.ready(). should be?
one way have code behind emit javascript array of labels. different--and approach take--would use class name "tag". emit:
<input type="text" class="lbllisting_" ...>
then in fixed (not dynamic) javascript, can do:
function getalllistings(){ var listings = document.getelementsbyclassname("lbllisting_"); (var = 0; < listings.length; i++) { var listingstring = listings[i].value; var guid = listingstring.split("_"); alert(guid[1]); } }
update follow-on question: javascript can placed anywhere not run on load. when , how run function depends on need do. (i assume alert
test logic.)
Comments
Post a Comment