2 Replies - 1559 Views - Last Post: 10 October 2012 - 10:42 AM

#1 Misfit  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 100
  • Joined: 18-July 09

Adding elements after the DOM has loaded

Posted 10 October 2012 - 06:58 AM

Hey all,

Bit of a nooby question here. Basically, I'm adding inputs to a form after the DOM has already loaded. I want to run validations (basically, strip dollar signs and round the numbers) and a checker to make sure only 20 of these fields may be loaded. I know just enough to recognize that neither of these are possible the way I'm trying since everything is already loaded and the functions aren't going to act on the new elements. I don't know much more about the problem, nor how to fix it.

	$(document).ready(function(){
		  //add up to 20 debts and assets
		  $("#before_debt").live('click', function(){ insertNode('primary[debt]', '#before_debt', $('#pri_debts').size('input') );})
		  $("#before_asset").live('click', function(){ insertNode('primary[addi]', '#before_asset', $('#pri_assets').size('input') );})
		  }

        //messy insert element function...
	function insertNode( name_prefix, button_id, counter ){
		var i = $('input[name^=\''+ name_prefix +'\']').last().attr('name').substr(14, 1);
		i = parseInt(i);
		i++;

		var is_debt = '';
		if( name_prefix == 'primary[debt]' ){
			is_debt = '<input type="hidden" name="primary[debt][' + i + '][is_debt]" value="1">';
		}

		if( counter <= 20 ){ //counter never increments
			$(button_id).before('<tr><td>'+ is_debt +'<input type="text" name="'+ name_prefix +'[' + i + '][account_name]" size="20" maxlength="40"></td><td>$<input type="text" class="strip" name="'+ name_prefix +'[' + i + '][account_value]" size="20" maxlength="10"></td></tr>');
		}
	}

//strip the dollar sign from all 'strip' class elements
	  $('.strip').blur(function() { 
		  $(this).val(function(index, val){
			    var stripped = val.replace('\$', '').trim();
			    return Math.round(stripped);
			});
	  });



Is This A Good Question/Topic? 0
  • +

Replies To: Adding elements after the DOM has loaded

#2 JITHU  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 62
  • View blog
  • Posts: 201
  • Joined: 02-July 07

Re: Adding elements after the DOM has loaded

Posted 10 October 2012 - 09:23 AM

If you're on Google Chrome, open up the Javascript console (on Windows Ctrl + Shift + J), reload the page and check if there are any errors.
Try clicking #before_debt, #before_asset elements, check if errors are coming up in the console.

If there are errors and you can't solve them yourself, try to post the error messages here and maybe also the html file.

You can do the same thing also on other browsers.
Was This Post Helpful? 0
  • +
  • -

#3 Misfit  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 100
  • Joined: 18-July 09

Re: Adding elements after the DOM has loaded

Posted 10 October 2012 - 10:42 AM

There aren't any errors in either chrome or firefox.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1