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.

Remove item from array if checkbox false

Discussion in 'jQuery' started by peter_c, Sep 24, 2019.

  1. #1
    With the code I have posted, I can add to array fine but how do I remove from array if checkbox is unticked. Any help would be greatfully appreciated. Many thanks

    $(function() {
    info = [];
    
    $(document).on('click', '.rowChk', function () {
    
    if($(this).is(':checked')) {
    $('#rowClk').show();
    
    var currentRows = $(this).closest("tr");
    var rackid = currentRows.find("td:eq(0)").html();
    
    data = {};
    
    data.id = rackid;
    
    info.push(data);
    //console.log(data);
    //console.log(info.length);
    } else {
    delete [info.id]; <--- Need to delete checkbox value from array -->
    //console.log(info.length);
    if(info.length === 0) {
    $('#rowClk').css('display','none');
    }
    }
    });
    });
    Code (javascript):
    PS. I cannot see a way to format the code in the editor. Is there an option?

    SEMrush
     
    Last edited by a moderator: Sep 24, 2019
    peter_c, Sep 24, 2019 IP
    SEMrush
  2. deathshadow

    deathshadow Acclaimed Member

    Messages:
    8,976
    Likes Received:
    1,635
    Best Answers:
    233
    Trophy Points:
    515
    #2
    1) I suggest disabling the scripttard editor on these forums and just using bbCode. bbCode is like HTML but with [] instead of <>. For formatting code, you just wrap your content in [code]Your code[/code]

    Thus:

    Your code
    Code (markup):
    https://en.wikipedia.org/wiki/BBCode

    2) It's hard to even figure out what your scripting is doing -- or even supposed to do -- without the markup it is manipulating.

    3) This is another STUNNING example of why the mental midgetry that is jQuery does nothing but piss on a website. The ONLY thing you can learn from it is how NOT to use JavaScript... for anything. EVER.

    IF I'm following your intent -- no guarantee on that -- then I'd be using an object instead of an array. It's equally iterable and easier to work with. I would also be trapping "change" and not "click", or perhaps both if I cared about legacy IE. (which I no longer do on scripting, let them gracefully degrade to the non-scripted version of the site.).

    Hence if info was {} instead of [], you could then do:

    
    info[Element.name] = Element.value; // set it
    delete info[Element.name]; // remove it
    
    Code (markup):
    Where Element is the actual checkbox INPUT. Remember JavaScript objects are enumerable so you can treat them much akin to PHP's indexed arrays.

    But given I can't even figure out what it is you're trying to do? Yeah, that's a problem. I mean why are you show/hiding? If it's hidden how do you unclick it? What makes the show/hide any of JavaScript's business? Could you leverage <label> and input:checked to do most of the heavy lifting?
     
    Last edited: Sep 24, 2019
    deathshadow, Sep 24, 2019 IP
  3. peter_c

    peter_c Greenhorn

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #3
    @deathshadow Thank you for reply. However, your comments have confused me. For example, the hide/show function is simply if a user clicks a checkbox then a action button is shown so they can action that row in the table. Unchecked, the button is hidden so they cannot enter null values etc.

    Why can you not figure out what I am trying to do? very simply, if a user clicks a checkbox, add the table row values to an array. If the checkbox is unchecked, then delete that value from the array. (Hope that is clearer).

    Could you do an example markup based on your comments. Many thanks.
     
    peter_c, Sep 24, 2019 IP
  4. deathshadow

    deathshadow Acclaimed Member

    Messages:
    8,976
    Likes Received:
    1,635
    Best Answers:
    233
    Trophy Points:
    515
    #4
    Why are you tracking them in an array if you've got a checkbox and perfectly good existing values? That's the part I'm not getting.

    It's funny, you asked for an example of markup, when I was going to suggest the same thing. I'm a "content oriented" developer, so I kind of need to see the data or a facsimile of data to even try and make sense of what you're saying. For right now, it sounds like you want to use JavaScript for something that sounds more like a server-side task... basically not even being any of JavaScript's business in the first place. (aka form data)
     
    deathshadow, Sep 25, 2019 IP
  5. peter_c

    peter_c Greenhorn

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #5
    Tell me what you need and i'll be happy to supply it. Many thanks
     
    peter_c, Sep 25, 2019 IP