Reputation: 8169
Upon clicking a button with dynamic id, I want to put dst.district
into a textbox with id district
.
I have written following code, but it's currently not working.
<input name="" type="text" class="dist1" id="district">
<% @districts.each do |dst|%>
<div id="dist_<%= dst.id%>" class="districtname"><%= dst.district %></div>
<input id="select_<%= dst.id%>" type="button" value="Select" class="gbutton" />
<script type="text/javascript">
$("#select_<%= dst.id %>").click({
$("#district").val("<%= dst.district %>");
});
</script>
<%end%>
Thank you in advance.
Upvotes: 0
Views: 869
Reputation: 3677
Can you use the class instead of the id? It may work. Try it and let me know.
<input name="select" id="select_<%= dst.id%>" type="button" value="Select" class="gbutton" />
<script type="text/javascript">
$(".gbutton").click(function(){
$("#district").val('the value you want into the textbox');
});
</script>
Upvotes: 0
Reputation: 1206
Just follow this code:
<input name="" type="text" class="dist1" id="district">
<% @districts.each do |dst|%>
<li>
<div class="districtname"><%= dst.district %></div>
<input name="select" id="select_<%= dst.id%>" type="button" value="Select" class="gbutton" />
</li>
<%end%>
and
$(".gbutton").click(function(){
var getID = this.id.split("_");
console.log(getID[1]);
});
Upvotes: 2