Reputation: 13
I need your help
I'm trying to add number to the onclick url number format of code is like this
<div class="message">
<div class="info"><a href="/user/spox/" onclick="ShowProfile('spox', '/user/spox/'); return false;" target="_blank">spox</a></div>
</div>
I have in source
var group_id = 1;
which is defend on what status has user admin, moder, publisher, user or guest each of them has individual number 1, 2, 3, 4, 5
so I need to add by javascript number of group_id in here
onclick="ShowProfile('spox', '/user/spox/', '1');
so format of adding number should be after '/user/some user name/' , 'group_id number'
I was trying to do this by code
$(function(){
attrUserA = $('.info a').attr('onclick');
attrUserA = attrUserA.split(';');
sliceUser = attrUserA[0].slice(0, -1);
attrUserA[0] = sliceUser + ", '" + group_id + "')";
attrUserA = attrUserA.join('; ');
$('.info a').attr('onclick', attrUserA);
});
it works, the script adds number from
var group_id = 1;
but I have problem with this, because script changes the individual urls in onclick to one so after this if some user had link like this
onclick="ShowProfile('spox', '/user/spox/', 'number from group id');
and last message was written by user: blablabla the individual personal urls of other users changed to
onclick="ShowProfile('blablabla', '/user/blablabla/', 'number from group id');
how can I just add number and don't touch urls?
Upvotes: 0
Views: 151
Reputation: 427
Do you mean something like this?:
jQuery:
var group_id = 1;
$(function(){
var attrUserA = $('.info a').attr('onclick');
attrUserA = attrUserA.split(',');
var sliceUser = attrUserA[0].slice(0, -1);
attrUserA[0] = sliceUser + "'";
attrUserA[1] = attrUserA[1].slice(0, -2);
attrUserA[1] = attrUserA[1] + ", " + group_id + ");";
attrUserA = attrUserA.join(', ');
$('.info a').attr('onclick', attrUserA);
alert(attrUserA);
});
HTML:
<div class="message">
<div class="info"><a href="/user/spox/" onclick="ShowProfile('spox', '/user/spox/');" target="_blank">spox</a>
</div>
</div>
http://jsfiddle.net/Blue_EyesWhiteDragon/tpdpgttn/
Upvotes: 1