1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Best way to handle 'click' for multiple devices?

Discussion in 'JavaScript' started by SoftLink, Jan 26, 2024.

  1. #1
    An html element can be clicked, touched, etc. depending on the device using it.
    Is there any way to handle them all without them crashing into each other?

    I used onpointerup but the label's for doesn't trigger it.
    Also, I need to pass the input element to the function.
    I have hundreds of these so I'm looking for a general solution.

    When I use onpointerup on a radio or checkbox input and the user checks it, the onpointerup function reads it as false. If the user clicks it again, it'll read true. It's backward.
    This tells me that when the first onpointerup fires the checkbox gets checked but not until after the onpointerup executes.

    How can I create a 'click' event (click, touch, etc) that will fire once if the input or the label is clicked, touched or what else - and - recognize any change (ie checked) in the input as a result of the event?

    
    
        <button onWHAT="doMyClick();" value="Click, Touch, Whatever Me" >
    
    Code (markup):

     
    SoftLink, Jan 26, 2024 IP
  2. hdewantara

    hdewantara Well-Known Member

    Messages:
    536
    Likes Received:
    47
    Best Answers:
    25
    Trophy Points:
    155
    #2
    Hi,
    sounds like you need an INPUT element of type CHECKBOX?
     
    hdewantara, Jan 26, 2024 IP
  3. SoftLink

    SoftLink Active Member

    Messages:
    102
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    60
    #3
    I actually almost edited it but didn't.

    OK now how do I make it work on all device types using click or touch or iduno voice command?
    
    
    <input id='ckTest' type="radio" onWHAT="doMyClick(this);" value="1" > <label for='ckTest' >Check the input</label>
    
    Code (markup):
     
    SoftLink, Jan 26, 2024 IP
  4. hdewantara

    hdewantara Well-Known Member

    Messages:
    536
    Likes Received:
    47
    Best Answers:
    25
    Trophy Points:
    155
    #4
    Hmm. I've never heard of HTML attribute ONWHAT before.
    This is the reason why some of your devices / browsers don't understand your line, perhaps?
    AFAIK, for INPUT element of type RADIO, you may use ONCLICK instead and test element's CHECKED attribute.
     
    Last edited: Jan 26, 2024
    hdewantara, Jan 26, 2024 IP
  5. SoftLink

    SoftLink Active Member

    Messages:
    102
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    60
    #5
    hdewantara: thank you but I don't think you can help me.
     
    SoftLink, Jan 26, 2024 IP
  6. SoftLink

    SoftLink Active Member

    Messages:
    102
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    60
    #6
    I figured this out. I thought a click event would not fire on touch.
    All good now - thanks.
     
    SoftLink, Jan 26, 2024 IP
  7. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    252
    Trophy Points:
    515
    #7
    Be warned I've had onclick not fire for input when you click on their label. You are using <label> like a good little doobie, right?

    In the case of checkboxes and radio sets, you might want to look at onchange and/or oninput... though the latter is more useful for actual text based inputs.

    And seriously, don't say onevent in your markup. It means you need a global scope function. It's called addEventListener, use it; this isn't 1997 no matter what the "framework stupid" gimboids claim.

    If the element itself is scripting only functionality, it probably doesn't belong in the markup either.
     
    deathshadow, Jan 28, 2024 IP
  8. SoftLink

    SoftLink Active Member

    Messages:
    102
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    60
    #8
    Thanks for the response Jason:
    "You are using <label>" yes on your previous recommendations.

    "don't say onevent in your markup"
    I put php -> javascript -> html all in the same file for one page (interface) with few exceptions.
    So, most of my javascript functions are global. Global scope is that page which is fine.
    I have cases where I use objects but the markup recognizes them: onclick='IfcUser.getRec(this.dataset.id);'.
    addEventListener is more work and adds code. I don't see the upside.

    "look at onchange and/or oninput"
    If I use an input or change event and the user clicks the label the function doesn't fire.
    Calling the function from the label doesn't work because the function needs the element or a value from it.

    "element itself is scripting only functionality" ah you know that trick. It's rare but I can't say I've never done it.
    I try to stick to dataset attributes.
     
    SoftLink, Jan 28, 2024 IP