0

I am writing PhoneGap application. I compile it using the PhoneGap Build web service. I have not included jQuery Mobile, though I have included jQuery in my project. In a pure web application I often use code like this:

<html>
    <body>
        <div class="button" id="button1">Hello</div>
        <script> 
            $(".button").click(function() {
                alert($(this).attr("id"));
            });
        </script>
    </body>
</html>

However, in PhoneGap, I find that when I reference any attribute of my $(".button") element via jQuery's $(this), the value of the attribute is undefined. What should I do?

Regent
  • 5,142
  • 3
  • 21
  • 35
GRY
  • 724
  • 3
  • 14
  • 33
  • If I reference anything by using $(this) the value is undefined. example: alert($(this).attr("id")); or alert($(this).html()); – GRY Apr 24 '15 at 06:12
  • Yes, Jquery is included. I am able to use JQuery's may features extensively throughout the page- just "$(this)" fails. – GRY Apr 24 '15 at 06:14
  • Strange. Did you use `jQuery(this)`? – kosmos Apr 24 '15 at 06:18
  • @kmsdev if `$(".button")` does work, there should not be any problems with `$(this)`. – Regent Apr 24 '15 at 06:22
  • I know, that's why I wrote "strange". That piece of code should work as expected (if jQuery has been loaded) – kosmos Apr 24 '15 at 06:50

1 Answers1

0

The event was not firing because the document was not ready. For phonegap I needed to register the event inside of the deviceReady() method. There is more about the event here: Phonegap deviceready vs document ready

Community
  • 1
  • 1
GRY
  • 724
  • 3
  • 14
  • 33