0

I have been bashing my face against a keyboard trying to work out why my javascript code doesnt work inside of ionic.

can someone please tell me the way to store a number inside of a variable, display that variable and increase that variable when you click a button..

I have this working no problems on a basic javascript game, but i am trying to incorporate it into ionic.. It is inside of a tab screen does that mean i have to write the javascript different?

the below code includes jquery and I don't think ionic will support that easily.

But if someone could help me, I just need a way to store a variable (most likely with localforage) and a way to display the variable (span?) and a way to increase taht variable with a click of a button? (with on-tap or onclick?)

Thanks heaps I have been bashing my face against the keyboard for 2 days now and it does not work...

It's a bit harder to link all my ionic code as it's over much more than just 3 files...

var coffee = localStorage.getItem("coffee") ?  localStorage.getItem("coffee") : 0.0;
var totalCoffee = localStorage.getItem("totalCoffee") ? localStorage.getItem("totalCoffee") : 0.0;


var cookRate = localStorage.getItem("cookRate") ?  localStorage.getItem("cookRate") : 1.0;


function prettify(input){
    var output = Math.round(input * 1000000)/1000000;
 return output;
}

$("#coffeeButton").click(function(e) {

    var obj = $("#clone").clone(); 
    var img = $("#myImg").clone(); 

    $("body").append(obj);
    $("body").append(img);

    obj.html("+"+ cookRate);
    coffee += cookRate;
    totalCoffee += cookRate;
    document.getElementById("coffee").innerHTML = prettify(coffee);
    document.getElementById("totalCoffee").innerHTML = prettify(totalCoffee);

    obj.css('position','absolute'); 
    obj.css('z-index', '2');
    img.css('position','absolute');
    img.show();

    obj.offset({left: e.pageX-10, top: e.pageY-80});
    img.offset({left: e.pageX-10, top: e.pageY-50});

    obj.animate({"top": "-=80px"}, 1000, "linear", function() {
        $(this).remove();
    }); 
    img.animate({"top": "-=80px"}, 1000, "linear", function() {
        $(this).remove();
    });

});
#coffeeButton{
 cursor: pointer; cursor: hand; background: #5EFF8F; border-radius: 7px; margin: 5px; padding: 20px; font: bold 30px Tahoma; text-align: left;
 -webkit-touch-callout: none; -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none; -user-select: none;
}

#clone{
 font-size: 1.5em;
 font-weight: bold;
 color: white;
 -webkit-touch-callout: none;
 -webkit-user-select: none; 
 -moz-user-select: none; 
 -ms-user-select: none; 
 -user-select: none;

}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<img title = "" id="myImg" src="images/cup.png" style="display:none" width="20" height="30">
      <div id="clone"></div>
   <div id = "coffeeButton">Make Coffee <br /><span id = "cookRate">1</span> Per Click</div>
   Coffee = <span id = "coffee">0.0</span>
MIke
  • 233
  • 3
  • 4
  • 12
  • That "button" isn't a button, it's a `
    ` (and therefore not keyboard accessible). Replace it with a real `
    – danielnixon Jan 10 '15 at 10:23
  • Also, `id`s must be unique within the document. – Teemu Jan 10 '15 at 10:24
  • [This actually works fine](http://jsfiddle.net/9nu8q6dj/). It's because this is in a snippet it has a security problem on SO. – Spencer Wieczorek Jan 10 '15 at 10:25
  • @danielnixon No that's not true, it's perfectly valid to add a click event to a div. – Spencer Wieczorek Jan 10 '15 at 10:27
  • SOrry i should mention i need a javascript only way to do this... Also i have rewritten it 10 different ways and yet it still doesn't work in ionic. – MIke Jan 10 '15 at 10:27
  • @SpencerWieczorek Yeah, I have had clickable divs many times, But in ionic i have it set as a button anyways using on-tap – MIke Jan 10 '15 at 10:28
  • @SpencerWieczorek only if you also add `role="button"`, `tabindex="0"` and handle `keydown` in addition to `click` events (which isn't the case here). – danielnixon Jan 10 '15 at 10:28
  • ALso I have tried using localforage not localstorage as i need it to work on ios and android.. – MIke Jan 10 '15 at 10:29
  • @SpencerWieczorek See http://www.w3.org/TR/WCAG20-TECHS/F59.html – danielnixon Jan 10 '15 at 10:29
  • @danielnixon http://api.jquery.com/click/ and note: *" Any HTML element can receive this event."*. – Spencer Wieczorek Jan 10 '15 at 10:31
  • I need javascript only.. no Jquery – MIke Jan 10 '15 at 10:33
  • @SpencerWieczorek Just because it's technically possible doesn't make it either semantically valid or accessible (it's neither). Please read the links in my comments. Try to trigger a click event on a div using *just* a keyboard if you still don't understand. – danielnixon Jan 10 '15 at 10:33
  • why hasnt anyone listed a way to do this in javascript... I know the above code works.. I stated that it does... I need this to be rewritten without jquery.. I have tried writting it 10 different ways and it doesn't work. – MIke Jan 10 '15 at 10:34
  • also as mentioned I don't need it to be a div... i just need a button that completes the function – MIke Jan 10 '15 at 10:35
  • How about posting the code of a your non-working non-jquery version? – JAL Jan 10 '15 at 10:38
  • Ill help you re-write this using plain js if need be, before we do though, can you try changing `$("#coffeeButton").click(function(e)` to `$("body").on("click", "#coffeeButton", function(e)` – Wesley Smith Jan 10 '15 at 10:40
  • I cant load jquery in ionic? – MIke Jan 10 '15 at 10:44
  • I cant post all my ionic code as its laid out a little different, it has an index.html which leads to a tab-main.html which links to app.js also services.js and controllers.js – MIke Jan 10 '15 at 10:45
  • here is a jsfiddle with some code i just wrote, but even this doesn't work properly http://jsfiddle.net/edznycyy/23/ – MIke Jan 10 '15 at 10:50
  • @DelightedD0D yes i will try that now, bear with me as i deleted all the code i tried but if i copy all the jquery code and paste it into a black ionic file it works, but when i try to design a new game using ionic and then go into an individual tab inside it doesn't work, so i feel like it's a linking issue but not sure. – MIke Jan 10 '15 at 10:52
  • lastly here is the code from ionic... not sure if some of it won;t make sense since i doubt jsfiddle will recognise it. http://jsfiddle.net/0zcg6t0w/ – MIke Jan 10 '15 at 10:54
  • see my answer below, it seems you can use jQuery in ionic but have to use `angular.element` instead of `$`. I have seen this in other instances before like when using jQuery inside of an atlassian confluence app. I cant test this but from what I read it should work.Check it on your end before we go vanilla javascript – Wesley Smith Jan 10 '15 at 11:01
  • @danielnixon Again, it's valid for the `.click()` event **in jQuery**. As such the library handles [event handlers properly](http://stackoverflow.com/questions/12627443/jquery-click-vs-onclick) because it was designed and implemented as so. Which is why you *can* trigger the click event by a keyboard without any problems. – Spencer Wieczorek Jan 10 '15 at 11:08
  • @SpencerWieczorek You're still mistaken. jQuery doesn't have magic powers. You simply *can't* make a div keyboard accessible without `tabindex="0"` (to allow it to be focused), `role="button"` (to fix its semantics) and an `onkeydown` (or jQuery's `keydown`) event handler to translate key presses into clicks (you can avoid having to do any of this by using a real ` – danielnixon Jan 10 '15 at 11:25
  • Thanks guys got it sorted :P really apprecaite the help, wish i had asked it this way 2 days ago – MIke Jan 10 '15 at 11:33
  • @danielnixon I've made my [point clear](http://jsfiddle.net/gzzoeoyk/1/) and am done debating this. If you wish to keep talking about this please open a chat to do so and not on this question. – Spencer Wieczorek Jan 10 '15 at 11:36
  • @SpencerWieczorek That fiddle is still inaccessible. Here's a [fixed version](http://jsfiddle.net/gzzoeoyk/4/). – danielnixon Jan 10 '15 at 11:49

1 Answers1

1

Try this, make sure jQuery is loaded before angular.js, and change all of your code from like this $('#coffeeButton') to like this angular.element('#coffeeButton') apparently, when used with angular.js the $ gets replaced with angular.element see http://forum.ionicframework.com/t/use-ionic-with-jquery/1120/8

You may need to put <script src="jquery.js"></script> above <script type="text/javascript" src="js/app.js"></script> in your html file.

var coffee = localStorage.getItem("coffee") ?  localStorage.getItem("coffee") : 0.0;
var totalCoffee = localStorage.getItem("totalCoffee") ? localStorage.getItem("totalCoffee") : 0.0;


var cookRate = localStorage.getItem("cookRate") ?  localStorage.getItem("cookRate") : 1.0;


function prettify(input){
    var output = Math.round(input * 1000000)/1000000;
    return output;
}

angular.element('#coffeeButton').click(function(e) {

    var obj = angular.element('#clone').clone(); 
    var img = angular.element('#myImg').clone(); 

    angular.element('body').append(obj);
    angular.element('body').append(img);

    obj.html("+"+ cookRate);
    coffee += cookRate;
    totalCoffee += cookRate;
    document.getElementById("coffee").innerHTML = prettify(coffee);
    document.getElementById("totalCoffee").innerHTML = prettify(totalCoffee);

    obj.css('position','absolute'); 
    obj.css('z-index', '2');
    img.css('position','absolute');
    img.show();

    obj.offset({left: e.pageX-10, top: e.pageY-80});
    img.offset({left: e.pageX-10, top: e.pageY-50});

    obj.animate({"top": "-=80px"}, 1000, "linear", function() {
        angular.element(this).remove();
    }); 
    img.animate({"top": "-=80px"}, 1000, "linear", function() {
        angular.element(this).remove();
    });

});
Wesley Smith
  • 17,890
  • 15
  • 70
  • 121
  • Thanks, I tried your code but now my porject doesn't even display... unsure why, the angular i need to load is just ionic-angular.js right? – MIke Jan 10 '15 at 11:08
  • Yeah, is `ionic-angular.js` loaded after jQuery? – Wesley Smith Jan 10 '15 at 11:13
  • yeah tried that still doesn't work with jquery.. also tried the above code and it doesnt work either... – MIke Jan 10 '15 at 11:18
  • It must be something to do with ionic.. it's hard to explain unless your familiar with it.. basically inside the WWW folder, if i place all the files from my original game inside it it works fine. but obviously isn't optimised for an android and doesn't display properly.. soon as i try using the same code inside of tab-main.html it doesn't work. – MIke Jan 10 '15 at 11:19
  • 1
    OMFG. i had to link the frigging file in index.html not tab-main.html!!! it works now at least in some form it works, at least now i can start reqriting it all... you have no idea ho long i have been bashing my head for... because the tab i am working in is loaded from index.html even though my page is being displayed from tab-main.html i needed to link jquery in index.html thanks! – MIke Jan 10 '15 at 11:29
  • I am giving the answer to @delighted as he made me check the source which led to fiixing the problem, but thanks to everyone who helped!!!! – MIke Jan 10 '15 at 11:30
  • @Mike in the end, did you have to use ` angular.element('body').append(obj);` or would `$('body').append(obj);` work? Just curious – Wesley Smith Jan 10 '15 at 18:37
  • I ended up just using $ since jquery started working as intended :P thanks again. life saver! – MIke Jan 11 '15 at 06:39