Javascript not working

  1. 7 years ago

    Hi. I am trying to have a button run a simple javascript when pressed. It works when I call the Objects by class but not when I call the object by ID (like the following). Am I doing something wrong? Thank you!

    $("#obj90").style.color="red";

  2. Angelo S

    17 Oct 2016 Administrator

    Hi,
    $("#obj90") uses JQuery and returns an array of objects. If you want to access DOM properties you must access to the first element of the array. For example, if "obj4155" is a rectangle, you can change its background color doing:

    $("#obj4155")[0].style.backgroundColor="red";

    Otherwise, you can use pure JQuery syntax like this:

    $("#obj4155").css("backgroundColor", "red");

    Best Regards,
    Angelo

  3. Got it. Thank you!

  4. As a follow up question... I used the following code on a button and it works to change the color of the button (and reveal certain items) when I press the button. However, it usually requires two clicks on the button to work instead of working on the first button. Am I doing something wrong? Thank you!

    var button = $("#obj157");
    var zhuyin = PubCoder.getObjectsWithClass("zhuyin");

    if (button[0].style.backgroundColor == "white") {

    button[0].style.backgroundColor = "gray";
    button[0].style.color = "white";
    for (i = 0; i < zhuyin.length; i++) {
    zhuyin[i].style.opacity = 1;
    }
    }

    else {

    button[0].style.backgroundColor = "white";
    button[0].style.color = "black";
    for (i = 0; i < zhuyin.length; i++) {
    zhuyin[i].style.opacity = 0;
    }
    }

  5. Angelo S

    17 Oct 2016 Administrator
    Edited 7 years ago by Angelo S

    Probably, though the button is white, the backgroundColor isn't "white" on the first click, maybe it's something like "#fff" or "#ffffff" or "rgba(255,255,255,1.0)", which are all equivalents. So the first click always catches the second brench of the IF. You try setting the color in the "on show" event, or maybe in your case just invert the if logic condition (e.g. if backgroundColor != white then set white else set grey)

  6. That worked. Thanks!

 

or Sign Up to reply!