0

Can anyone see where I going wrong here, I'm trying to get the last characters after the ?p= in a link and put in a varaible, but the code I'm using returns /.

My code is:

 $("#dvPartial").on('click', '.dvPagerCities a', function (event) {
        alert('click detected');
        var city = ($('input#hdnCountry').val());
        alert(city);
        var link = $('a').attr('href');
        //var getEqualPosition = link.indexOf('?p='); //Get the position of '='
        var getEqualPosition = link.indexOf('='); //Get the position of '='
        var number = link.substring(getEqualPosition + 1); //Split the string and get the number.

My link is

<a href="/Weather/Index/Australia?p=2">»</a>

I think what is happening is its picking up the 1st = .

My theory is this.

1) Detect the click event

2) Get the link that caused the event

3) Extract the value of p,

p can be 1 diget, 2 digets or 3 digets.

Any help would be appreciated, as it seems no sooner do I solve 1 problem then another arises.

Thanks

George

CareerChange
  • 669
  • 4
  • 17
  • 34

5 Answers5

1

Assuming that p is the only parameter in the URL you can simply split by the = sign:

$("#dvPartial").on('click', '.dvPagerCities a', function (event) {
    var city = $('input#hdnCountry').val();
    var number = $(this).attr('href').split('=')[1];
});
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
1

The problem is this part:

var link = $('a').attr('href');

That does not select the link that was clicked, unless you happen to be clicking the very first link present in the HTML of the page. What that will do is select all <a> elements, then return the href attribute of the first one.

Inside your event handler, you want to use this to refer to the link that was clicked:

var link = $(this).attr('href');
// or simply
var link = this.href;
Anthony Grist
  • 38,173
  • 8
  • 62
  • 76
  • Hi Anthony, that for the theory, i'm having to learn a lot in a short space of time, awarded a point up for teaching me something, thanks – CareerChange Feb 07 '13 at 11:24
0

Try with this:

link.split('?p=')[1]

sohel khalifa
  • 5,602
  • 3
  • 34
  • 46
0

Try this: http://jsfiddle.net/7atyG/1/

$(document).on('click', '.dvPagerCities a', function (ev) {
   ev.preventDefault();
   var link = $(this).attr('href');
   var Linkval = link.substr(link.indexOf('=') + 1);
   alert(Linkval);
});
Jai
  • 74,255
  • 12
  • 74
  • 103
0
String s = "Jecy penny ? like sears";

String[] arr = s.split("\\?");  

Added \\ before ?, as ? has a special meaning

String res = arr[0];

May be you are wrong at escape character for ' ? '

check this link Remove all characters after a particular character

Community
  • 1
  • 1
sasi
  • 4,192
  • 4
  • 28
  • 47