Reputation: 85
Here's what I'm trying to do. I'm trying to get a block of code written in div then modify some of the div/id to different names and so I can send them to the form with unique names. Any ideas on how to do this? My idea right now is to first set the variable to to the div that I'm copying then access the other div's within it, but that doesn't seem to work. Any ideas?
var i = 0;
$("#custom_rates").click(function()
{
var sublet_dates_seen = $("#sublet_dates_seen").clone();
// all id's below are within #sublet_dates_seen
sublet_dates_seen.getElementById('#CustomPricePerNightPrice').attr('name','data[CustomPricePerNight][price][' + i ']');
sublet_dates_seen.getElementById(('#CustomPricePerNightStartDateMonth').attr('name','data[CustomPricePerNight][start_date][month][' + i ']');
sublet_dates_seen.getElementById(('#CustomPricePerNightStartDateDay').attr('name','data[CustomPricePerNight][start_date][day][' + i ']');
sublet_dates_seen.getElementById(('#CustomPricePerNightStartDateYear').attr('name','data[CustomPricePerNight][start_date][year][' + i ']');
sublet_dates_seen.getElementById(('#CustomPricePerNightEndDateMonth').attr('name','data[CustomPricePerNight][end_date][month][' + i ']');
sublet_dates_seen.getElementById(('#CustomPricePerNightEndDateDay').attr('name','data[CustomPricePerNight][end_date][day][' + i ']');
sublet_dates_seen.getElementById(('#CustomPricePerNightEndDateYear').attr('name','data[CustomPricePerNight][end_date][year][' + i ']');
sublet_dates_seen.getElementById(('#CustomPricePerNightMinimumNights').attr('name','data[CustomPricePerNight][minimum_nights][' + i ']');
sublet_dates_seen.getElementById(('#CustomPricePerNightMaximumNights').attr('name','data[CustomPricePerNight][maximum_nights][' + i ']');
sublet_dates_seen.appendTo(".avi_specialrates");
i = i + 1;
return false;
});
Upvotes: 0
Views: 189
Reputation: 1262
Your best bet is to replace your getElementById calls with jQuery's .find() method: http://api.jquery.com/find/
Also, I believe .clone() returns an HTML element or set of elements, not a jQuery object, so you'd have to do something like this:
$(sublet_dates_seen).find('#CustomPricePerNightPrice').attr('name','data[CustomPricePerNight][price][' + i ']');
Upvotes: 0
Reputation: 348972
You're trying to mix DOM and jQuery in a wrong way. The .find()
method has to be used. Also, you've got too much parentheses at the middle of your code.
sublet_dates_seen.getElementById('#CustomPricePerNightPrice'); //WRONG
sublet_dates_seen.find('#customPricePerNight'); //Correct
The correct code:
var i = 0;
$("#custom_rates").click(function()
{
var sublet_dates_seen = $("#sublet_dates_seen").clone();
// all id's below are within #sublet_dates_seen
sublet_dates_seen.find('#CustomPricePerNightPrice').attr('name','data[CustomPricePerNight][price][' + i ']');
sublet_dates_seen.find('#CustomPricePerNightStartDateMonth').attr('name','data[CustomPricePerNight][start_date][month][' + i ']');
sublet_dates_seen.find('#CustomPricePerNightStartDateDay').attr('name','data[CustomPricePerNight][start_date][day][' + i ']');
sublet_dates_seen.find('#CustomPricePerNightStartDateYear').attr('name','data[CustomPricePerNight][start_date][year][' + i ']');
sublet_dates_seen.find('#CustomPricePerNightEndDateMonth').attr('name','data[CustomPricePerNight][end_date][month][' + i ']');
sublet_dates_seen.find('#CustomPricePerNightEndDateDay').attr('name','data[CustomPricePerNight][end_date][day][' + i ']');
sublet_dates_seen.find('#CustomPricePerNightEndDateYear').attr('name','data[CustomPricePerNight][end_date][year][' + i ']');
sublet_dates_seen.find('#CustomPricePerNightMinimumNights').attr('name','data[CustomPricePerNight][minimum_nights][' + i ']');
sublet_dates_seen.find('#CustomPricePerNightMaximumNights').attr('name','data[CustomPricePerNight][maximum_nights][' + i ']');
sublet_dates_seen.appendTo(".avi_specialrates");
i = i + 1;
return false;
});
Upvotes: 2