javascript - How to populate a select option with value and text -


i trying populate select option this:

function listaano(){ var lista = $("#ano"); var currentyear = (new date).getfullyear(); var pastyear = 2000; var rangeyear = currentyear - pastyear;  ( var j = 0; j <= rangeyear; j++ ){     (var = currentyear; >= pastyear; i-- ){         lista.append($("<option/>").val(j).text(i));     } } 

i'm getting this:

    <select id="ano" name="ano"><option>año</option><option value="0">2013</option><option value="0">2012</option><option value="0">2011</option><option value="0">2010</option><option value="0">2009</option><option value="0">2008</option><option value="0">2007</option><option value="0">2006</option><option value="0">2005</option><option value="0">2004</option><option value="0">2003</option><option value="0">2002</option><option value="0">2001</option><option value="0">2000</option><option value="1">2013</option><option value="1">2012</option><option value="1">2011</option><option value="1">2010</option><option value="1">2009</option><option value="1">2008</option><option value="1">2007</option><option value="1">2006</option><option value="1">2005</option><option value="1">2004</option><option value="1">2003</option><option value="1">2002</option><option value="1">2001</option><option value="1">2000</option><option value="2">2013</option><option value="2">2012</option><option value="2">2011</option><option value="2">2010</option><option value="2">2009</option><option value="2">2008</option><option value="2">2007</option><option value="2">2006</option><option value="2">2005</option><option value="2">2004</option><option value="2">2003</option><option value="2">2002</option><option value="2">2001</option><option value="2">2000</option><option value="3">2013</option><option value="3">2012</option><option value="3">2011</option><option value="3">2010</option><option value="3">2009</option><option value="3">2008</option><option value="3">2007</option><option value="3">2006</option><option value="3">2005</option><option value="3">2004</option><option value="3">2003</option><option value="3">2002</option><option value="3">2001</option><option value="3">2000</option><option value="4">2013</option><option value="4">2012</option><option value="4">2011</option><option value="4">2010</option><option value="4">2009</option><option value="4">2008</option><option value="4">2007</option><option value="4">2006</option><option value="4">2005</option><option value="4">2004</option><option value="4">2003</option><option value="4">2002</option><option value="4">2001</option><option value="4">2000</option><option value="5">2013</option><option value="5">2012</option><option value="5">2011</option><option value="5">2010</option><option value="5">2009</option><option value="5">2008</option><option value="5">2007</option><option value="5">2006</option><option value="5">2005</option><option value="5">2004</option><option value="5">2003</option><option value="5">2002</option><option value="5">2001</option><option value="5">2000</option><option value="6">2013</option><option value="6">2012</option><option value="6">2011</option><option value="6">2010</option><option value="6">2009</option><option value="6">2008</option><option value="6">2007</option><option value="6">2006</option><option value="6">2005</option><option value="6">2004</option><option value="6">2003</option><option value="6">2002</option><option value="6">2001</option><option value="6">2000</option><option value="7">2013</option><option value="7">2012</option><option value="7">2011</option><option value="7">2010</option><option value="7">2009</option><option value="7">2008</option><option value="7">2007</option><option value="7">2006</option><option value="7">2005</option><option value="7">2004</option><option value="7">2003</option><option value="7">2002</option><option value="7">2001</option><option value="7">2000</option><option value="8">2013</option><option value="8">2012</option><option value="8">2011</option><option value="8">2010</option><option value="8">2009</option><option value="8">2008</option><option value="8">2007</option><option value="8">2006</option><option value="8">2005</option><option value="8">2004</option><option value="8">2003</option><option value="8">2002</option><option value="8">2001</option><option value="8">2000</option><option value="9">2013</option><option value="9">2012</option><option value="9">2011</option><option value="9">2010</option><option value="9">2009</option><option value="9">2008</option><option value="9">2007</option><option value="9">2006</option><option value="9">2005</option><option value="9">2004</option><option value="9">2003</option><option value="9">2002</option><option value="9">2001</option><option value="9">2000</option><option value="10">2013</option><option value="10">2012</option><option value="10">2011</option><option value="10">2010</option><option value="10">2009</option><option value="10">2008</option><option value="10">2007</option><option value="10">2006</option><option value="10">2005</option><option value="10">2004</option><option value="10">2003</option><option value="10">2002</option><option value="10">2001</option><option value="10">2000</option><option value="11">2013</option><option value="11">2012</option><option value="11">2011</option><option value="11">2010</option><option value="11">2009</option><option value="11">2008</option><option value="11">2007</option><option value="11">2006</option><option value="11">2005</option><option value="11">2004</option><option value="11">2003</option><option value="11">2002</option><option value="11">2001</option><option value="11">2000</option><option value="12">2013</option><option value="12">2012</option><option value="12">2011</option><option value="12">2010</option><option value="12">2009</option><option value="12">2008</option><option value="12">2007</option><option value="12">2006</option><option value="12">2005</option><option value="12">2004</option><option value="12">2003</option><option value="12">2002</option><option value="12">2001</option><option value="12">2000</option><option value="13">2013</option><option value="13">2012</option><option value="13">2011</option><option value="13">2010</option><option value="13">2009</option><option value="13">2008</option><option value="13">2007</option><option value="13">2006</option><option value="13">2005</option><option value="13">2004</option><option value="13">2003</option><option value="13">2002</option><option value="13">2001</option><option value="13">2000</option></select> 

how can value + text each year?

like this:

<option value="0">2013</option> <option value="1">2012</option> .... <option value="13">2000</option> 

i've been reading , it's supposed need use break or continue not sure, don't know how use it. i'm new in web programming, thank you.

you not need 2 for loops this. can as:

for (var = currentyear; >= pastyear; i-- ){     lista.append($("<option/>").val(currentyear-i).text(i)); } 

see demo


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -