0

Best explained with an example you can fiddle with: http://jsfiddle.net/F4H46/

To summarize:

(a) JQuery script is fired by clicking an anchor tag.

(b) Desired result: get chars following the href id=? and save to variables

(c) $(this)[0] does contain the correct href

(d) Using .match(regex) to strip off the desired chars creates a 404 error.

Why a 404 error? The .match(regex) works PERFECTLY if the same string is hard-coded.

0

3 Answers 3

1
$("a").bind('click', function(e) {
    e.preventDefault();  
    var xxx = $(this).attr('href');
    alert(xxx);
    var yyy = xxx.match(/\=(\d*)(\w*)/);
    alert(yyy[0]);
    alert(yyy[1]);
    alert(yyy[2]);
});
Sign up to request clarification or add additional context in comments.

Comments

0

When you click the anchor tag, the page will automatically change the href of the page, causing a 404 cause the page doesn't exist. First thing to do after the anchor tag is clicked is: e.preventDefault(), preventing the page from getting redirected.

$("a").click(function(e){
    e.preventDefault();
    // rest of the code
});

1 Comment

Oh right! I remember telling myself not to forget preventDefault when I read the api...
0

Youse this:

<script type="text/javascript">
$("a").click(function()
{   var xxx = $(this).attr('href');
        alert(xxx);
    var yyy = xxx.match(/\=(\d*)(\w*)/);
    alert(yyy[0]);
    alert(yyy[1]);
    alert(yyy[2]);
});
</script>

This working, difference is that you need to use attr('href');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.