tomtimms Posted May 12, 2011 Share Posted May 12, 2011 I am trying to append a value to the end of my url however getting not a function error. any help? q('#billing_address_setting').attr('href').append('site_id'+data); Quote Link to comment https://forums.phpfreaks.com/topic/236268-append-to-url/ Share on other sites More sharing options...
JasonLewis Posted May 13, 2011 Share Posted May 13, 2011 Unfortunately I don't believe you can append that easily to a URL. Well, it's still easy but not like that. $('#billing_address_setting').attr('href', $('#billing_address_setting').attr('href') + 'site_id' + data); What is 'q'? You're using jQuery correct? Quote Link to comment https://forums.phpfreaks.com/topic/236268-append-to-url/#findComment-1214790 Share on other sites More sharing options...
tomtimms Posted May 13, 2011 Author Share Posted May 13, 2011 I get Error: $("#billing_address_setting") is null, and yes jquery q should be $ Quote Link to comment https://forums.phpfreaks.com/topic/236268-append-to-url/#findComment-1214798 Share on other sites More sharing options...
JasonLewis Posted May 13, 2011 Share Posted May 13, 2011 If you're getting that, have you made sure that this link you are trying to append to has the correct ID. <a href="http://www.somelinkhere.com/" id="billing_address_setting">Link text</a> Quote Link to comment https://forums.phpfreaks.com/topic/236268-append-to-url/#findComment-1214802 Share on other sites More sharing options...
tomtimms Posted May 13, 2011 Author Share Posted May 13, 2011 It sure does, totally stumped. Quote Link to comment https://forums.phpfreaks.com/topic/236268-append-to-url/#findComment-1214804 Share on other sites More sharing options...
JasonLewis Posted May 13, 2011 Share Posted May 13, 2011 I ran this on my local machine and it worked flawlessly. Can you post your entire code? Something may be buggering it up... Quote Link to comment https://forums.phpfreaks.com/topic/236268-append-to-url/#findComment-1214809 Share on other sites More sharing options...
tomtimms Posted May 13, 2011 Author Share Posted May 13, 2011 So I got it to work, however I am using a button to save and each time I save it keeps adding what I want to the end, I just need one instance of it. So code $('#billing_address_setting').attr('href', $('#billing_address_setting').attr('href') + 'site_id' + data); create http://www.mysite.com?a=100 and each time I click save it keeps adding 100, so it now looks like www.mysite.com?a=100100100 etc. How can I make it just post 1 time? Quote Link to comment https://forums.phpfreaks.com/topic/236268-append-to-url/#findComment-1214812 Share on other sites More sharing options...
jcanker Posted May 13, 2011 Share Posted May 13, 2011 jQuery's great for chaining and slapping a whole bunch of stuff together in one line, but in this particular instance, you're probably better off breaking out a var first, resetting it to 0 or null and then setting the value. Your current code is taking the existing value and adding to it. Click it again and it takes the NEW value and adds to it. Click it again....etc. Take the value that's being increased accidentally, set it as its own var and reset it before appending it to the url. Should work. Quote Link to comment https://forums.phpfreaks.com/topic/236268-append-to-url/#findComment-1214819 Share on other sites More sharing options...
JasonLewis Posted May 13, 2011 Share Posted May 13, 2011 Depending on how you do it, you could for instance have the function bound to the click event of the button. For example: $('#button').click(function(){ $('#billing_address_setting').attr('href', $('#billing_address_setting').attr('href') + 'site_id' + data); // Now unbind the click event. $(this).unbind('click'); }); Or you could simply check to see if the URL already has ?a=100 at the end of it by using .indexOf() if($('#billing_address_setting').attr('href').indexOf(data) == -1){ $('#billing_address_setting').attr('href', $('#billing_address_setting').attr('href') + 'site_id' + data); }[/code] Quote Link to comment https://forums.phpfreaks.com/topic/236268-append-to-url/#findComment-1214820 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.