0

I have a input field that has the id #formValueId

<input id="formValueId" type="text" class="form-control" placeholder="email address" name="unsubemail">

I'm using this function here as well,

function addURL(element) {
$(element).attr('href', function(){
    return this.href + 'unsubscribe-complete?q=direct_unsubscribe&fn=Public_DirectUnsubscribeForm&id=IDSTRING&' + 'unsubscribeEmail';
});
}

I also created a variable like this,

var unsubscribeEmail = $("#formValueId").val();

at the end of my addURL function for return this.href line I'm adding the variable to the end of it and it works great but the url isn't passing the val(); it's just putting the var name at the end. what am I doing wrong here?

+ 'unsubscribeEmail';

gx2g
  • 319
  • 5
  • 18
  • 1
    _“what am I doing wrong here?”_ – you’re adding a _string value_ that contains the _text_ `unsubscribeEmail` … – CBroe Dec 14 '15 at 23:16

1 Answers1

1

by using 'unsubscribeEmail' you make it just string so use unsubscribeEmail instead of 'unsubscribeEmail'

function addURL(element) {
$(element).attr('href', function(){
    return this.href + 'unsubscribe-complete?q=direct_unsubscribe&fn=Public_DirectUnsubscribeForm&id=IDSTRING&' + unsubscribeEmail;
});
}
Mohamed-Yousef
  • 23,946
  • 3
  • 19
  • 28
  • I'm getting IDSTRING&undefined now is that because it's not in a document ready function? looks l it's working just isn't passing the val to the url say's undefined a the end. – gx2g Dec 14 '15 at 23:32
  • @gx2g better to use your code inside document ready .. and it works here http://jsfiddle.net/mohamedyousef1980/sm9onvyh/ .. if it not work with you or give you undefined again just update this demo with your related code and send it back to me – Mohamed-Yousef Dec 14 '15 at 23:45
  • says the addURL is undefined i have the – gx2g Dec 15 '15 at 00:00
  • – gx2g Dec 15 '15 at 00:15
  • @gx2g its working but not on load .. its working wrap on head after jquery liberary include or wrap before

    http://jsfiddle.net/mohamedyousef1980/sm9onvyh/2/

    – Mohamed-Yousef Dec 15 '15 at 00:17
  • @gx2g you can use it like this instead of onclick= http://jsfiddle.net/mohamedyousef1980/sm9onvyh/3/ this will work with you .. Note: id must be unique so don't use same id for more than one element – Mohamed-Yousef Dec 15 '15 at 00:43