2

The following code gives me an alert with nothing but a # symbol inside it. Why?

EDIT: I should note that the code is inside a jQuery .click event...if I place it outside of that, it works properly. Here is the fuller code:

 $('#continue').click(function(){
                                 var _href = $("#continue").attr("href");
                                 alert(_href);                                
            });


<a href="selectRadius.html" data-icon="arrow-r" id="continue" style="float:right;" data-role="button" data-inline="true">Continue</a>

EDIT2: This all works fine in jsfiddle. But in the xcode iphone simulator I just get #.

17
  • Working here: jsfiddle.net/CFpeU/3 hope you ar including <script> tag like this: jsfiddle.net/CFpeU/5
    – Tats_innit
    May 27, 2012 at 2:48
  • Are you using jQuery Mobile et jQuery UI, by any chance ?
    – HoLyVieR
    May 27, 2012 at 2:48
  • Mobile. And yes, yes, I'm using my script tag, lol. Your fiddle is identical to my code...confusing. This is being tested inside the xcode iPhone simulator, and giving different results. Granted, jQuery Mobile is involved, so... May 27, 2012 at 2:50
  • When I use the exact same code in an inline script rather than in head, it alerts the right value. Bizarre. May 27, 2012 at 2:52
  • 1
    I seem to remember reading something about how the iPhone browser raises 'tap' events rather than 'click' events, but bugger if I can't find the bookmark... :p
    – Tieson T.
    May 27, 2012 at 3:03

3 Answers 3

3

Judging by only the code you typed, probably the code runs too early. Try wrapping the JS in

$(function() {
    // your code here
});

Or

$(document).ready(function(){ 
    // your code here
});

Update:

Well, since it's an iPhone simulator, it changes things. Remember, nobody can help you unless you give all the details of the problem, no matter how much experience they have.

Did you try the touchstart / touchend / tap events instead of click? As far as I know, Apple has been having problems with the click events. Also, click events on mobile devices will have a slower response (a delay of approx 300ms if I remember well) so you're better just using touch specific events.

What are you building? Is it a mobile web app or? Will it run in a standard mobile browser or something like PhoneGap etc?

Update2:

Ok. It works as long as the code is not called on Click. This eliminates the possibility of another piece of code replacing your "href" with another value because that code would have to be inside your $('#continue').click(function(){ }); block.

The click event is simulated on a touch phone, that's why the touch events are faster (they are native) and less likely to cause problems. You should also make sure that you return false there and not follow the link, that might be what's replacing your "href".

6
  • I doubt it. It can't find #continue because it's not there yet. You need to not call click until the DOM has loaded, hence the use of $(document).ready...
    – Mark Reed
    May 27, 2012 at 3:00
  • To be honest, it's already in that. This is part of a larger script which is all inside $(function(){code here}); May 27, 2012 at 3:01
  • @MarkReed Assuming his code alert "#", it found the element otherwise it would have alert "undefined". The problem is finding out what replaced the href of the element to "#".
    – HoLyVieR
    May 27, 2012 at 3:03
  • Oh, d'oh, he's getting the alert popup, just with the wrong value. Sorry.
    – Mark Reed
    May 27, 2012 at 3:04
  • I'm sorry for the incomplete information. My mistake. But as far as I know, .click works fine. I've been using it extensively without any errors. May 27, 2012 at 3:36
1
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
<script>
$(document).ready(function(){ 
    $('#continue').click(function(e) {
        var _href = $(this).attr('href');
        alert(_href);

        e.preventDefault();
        return(false);
        /*
           the return is legacy code, used by some
           browsers to detect if current event handler
           is braking default behaviour

           the e.preventDefault() function is the jQuery
           way to do it
        */
    });
});
</script>

<a href="selectRadius.html" data-icon="arrow-r" id="continue" style="float:right;" data-role="button" data-inline="true">Continue</a>

Without this line the link is followed and a refresh occurs killing the current script.

3
  • Unfortunately this did not solve the problem. I still get a hash sign. May 27, 2012 at 3:18
  • As others have said, the href of the continue button might have been replaced to # by some piece of code in another part of the application. Search your entire project for #continue and see if it's been mentioned anywhere else. May 27, 2012 at 3:30
  • Problem found. See my answer. I'm going to use a form instead of an anchor as a work-around for the time being. May 27, 2012 at 3:44
0

https://github.com/jquery/jquery-mobile/issues/3777

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.