4 Replies - 11899 Views - Last Post: 28 February 2012 - 10:38 AM

#1 tbare   User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 47
  • Joined: 29-July 11

concat a string to get an ID

Posted 28 February 2012 - 08:37 AM

I'm creating a site that loops through a bunch of quotes for different listed by customer, giving sales reps a way to track pending quotes.

I'm trying to get a checkbox to insert today's date into a corresponding textbox, but seem to be having some trouble with getting it to work.

the format of the IDs on the checkboxes and text inputs is id="emailCheckbox[x][y]" and id="emailDate[x][y]" respectively.

what i'm trying to do is pull the [x][y] from the checkbox that was checked (working), and append it to "emailDate" to make "emailDate[x][y]", and update .val()

here's my relative code:
<div>
	<div class="emailCheckbox">Emailed: <input type="checkbox" id="emailCheckbox[0][1]" name="emailCheckbox[0][1]" value="emailCheckbox[0][1]" /></div>
	<div class="emailDate">Date: <input type="text" id="emailDate[0][1]" name="emailDate[0][1]" value="" size="15" /></div>			
</div>


and the javascript:
$(document).ready(function(){ 	
	var today = new Date();
	var dd = today.getDate();
	var mm = today.getMonth()+1; //January is 0!

	var yyyy = today.getFullYear();
	if(dd<10){dd='0'+dd} if(mm<10){mm='0'+mm} var today = mm+'/'+dd+'/'+yyyy;

	$('div.emailCheckbox :checkbox').change(function(){
        if($(this).attr('checked'))
        {
		var str = $(this).attr('id')
		var CustQuote = str.substr(str.indexOf("[")); // Contains [x][y] for customer and quote//
		alert(CustQuote + " checked " + today);
		alert($("#emailDate" + CustQuote).attr('id'));
        }
    });
});	



well, all i'm getting is "undefined" right for: alert($("#emailDate" + CustQuote).attr('id'));

keep in mind that there is much more information being displayed (also another checkbox that will be doing the same thing for "called[x][y]" and a corresponding text field for "callDate[x][y]" - the parent div has a class of "even" or "odd", and all of the [x][y] values are being filled in with php when the quotes are calculated.

I'm not opposed to changing the div layout if it makes it easier - i'm just kind of on a time crunch, and don't have the time to spend a day trying to get this to work.

..and it's driving me nuts... any thoughts?

Is This A Good Question/Topic? 0
  • +

Replies To: concat a string to get an ID

#2 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4303
  • View blog
  • Posts: 13,677
  • Joined: 08-June 10

Re: concat a string to get an ID

Posted 28 February 2012 - 09:10 AM

what does the previous line alert?
Was This Post Helpful? 0
  • +
  • -

#3 tbare   User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 47
  • Joined: 29-July 11

Re: concat a string to get an ID

Posted 28 February 2012 - 09:14 AM

"[6][2] checked 02/28/2012" -- obviously, [x][y] = [6][2] in this case...
Was This Post Helpful? 0
  • +
  • -

#4 tbare   User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 47
  • Joined: 29-July 11

Re: concat a string to get an ID

Posted 28 February 2012 - 10:15 AM

I think I figured it out -- I was curious if the square brackets were breaking it, and found that others were having the same issue -- i replaced the brackets with colons in the names / ids, and changed the javascript to the following:
$('div.emailCheckbox :checkbox').change(function(){
	if($(this).attr('checked'))
	{
		var str = $(this).attr('id')
		var CustQuote = str.substr(str.indexOf(":")); // Contains :0:3 or :2:1, etc..//
		$("input[name='emailDate" + CustQuote + "']").val(today);
	}
});



and now my date field is being populated... may not be the BEST way to get it to work, but it's working - i'll do what i can to polish it later, but wanted to let you know I got it working.
Was This Post Helpful? 0
  • +
  • -

#5 tbare   User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 47
  • Joined: 29-July 11

Re: concat a string to get an ID

Posted 28 February 2012 - 10:38 AM

EDIT:
on further searching - apparently it WASN'T the square brackets -- changing them back, but specifying that:
$("input[id='emailDate" + CustQuote + "']").val(today);

was the key... i was trying to set the value of the ID, not the input with the id, as well as the single quotes around the id in the double quotes in the $() allowed me to use the proper concatenation needed.

Hope this helps someone else
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1