robbinj
robbinj

Reputation: 41

jQuery changing attr adds a blank?

Trying to change the attr of something, I've got everything working but it adds a blank between the '=' and the 'data' and I really don't know why. I was hoping someone could help me out.

function get_delete_news(news_id) {
$.post('remove_get_news.php', {news_id:news_id}, function(data) {
    $('.confirmYes').attr('href', 'remove_news.php?newsID='+data);
});
}

So right now it's written out like this: remove_news.php?newsID= 2

And I want it to be like this: remove_news.php?newsID=2

Upvotes: 2

Views: 78

Answers (2)

izilotti
izilotti

Reputation: 4927

Apparently remove_get_news.php is concatenating a space to its response. A workaround would be to do a:

data.replace(/(^\s+|\s+$)/g, ''); 

before using the data variable.

Upvotes: 0

j08691
j08691

Reputation: 207901

The .trim() function will remove leading and trailing whitespace on a string.

Change:

$('.confirmYes').attr('href', 'remove_news.php?newsID='+data);

to:

$('.confirmYes').attr('href', 'remove_news.php?newsID='+data.trim());

Upvotes: 2

Related Questions