0

i was wondering how I could check to see if my href contains a string within the click function? I've tried different ways and have looked at other similar questions, but I can't seem to get it working.. I've tried contains and also regexp.. Here's the code:

$.each($ul.find('a'),function(){
  $(this).click(function(e){
    e.preventDefault();
    if (  ) { // this is where i've been trying check whether the href contains 'cid'...
      alert( 'your href contains cid' );
    } else {
      alert(' do nothing');
    };
  });
});

1 Answer 1

3

Use the indexOf method for it:

if($(this).attr('href').indexOf('cid') != -1)

You do not need .each() btw:

$ul.find('a').click(function(e) {
    //...
});
Sign up to request clarification or add additional context in comments.

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.