-1

I have a input like this, how do i remove particular date like 2018-01-03 from below comma separated values using jquery

<input type="text" id="selecteddates" value="2018-01-11,2018-01-03,2018-01-27,2018-01-31">



 $('.input_fields_container').on("click",".remove_field", function(e){ 
    //user click on remove text links

           //here iam getting date value
            var removedate=$(this).attr('id');
            var newselectedDates=$('#selecteddates').val();
            //newselectedDates.replace(new RegExp(removedate + ',?'), '');


            var entryArray = newselectedDates.split(",");



            return false;
            e.preventDefault(); $(this).parent('div fieldset').remove(); x--;
        })
user3544256
  • 201
  • 1
  • 3
  • 9
  • Start by splitting the value by `,`. – ADreNaLiNe-DJ Jan 08 '18 at 09:31
  • split it into an array (by the commas) and then find the one you want and remove it. Then combine back into a string if necessary. Did you try anything at all yet? – ADyson Jan 08 '18 at 09:32
  • can you provide fiddle or code – user3544256 Jan 08 '18 at 09:33
  • Urgh, why do you have data like that in a text box? – Alex Jan 08 '18 at 09:33
  • no man, iam not saving those values in db, based on that append html to div. – user3544256 Jan 08 '18 at 09:35
  • @user3544256 could you provide fiddle for what you have tried? – Himanshu Tyagi Jan 08 '18 at 09:37
  • for example if date is exist iam not appending html to div, if date not exist iam appending html to div, – user3544256 Jan 08 '18 at 09:37
  • 2
    Doing a quick search for what people have said in comments, "Split string by comma" and "Build a string from an array" will give you everything you need. If we write it for you you'll learn nothing, you've been handed the solution, you just have to understand it and write it. – DBS Jan 08 '18 at 09:38
  • https://stackoverflow.com/questions/11757671/split-comma-separated-input-box-values-into-array-in-jquery-and-loop-through-it/11757817 please keep a habit of searching the solutions. If you don't get after a big effort then ask questions. @AlexThomas. i've updated my answer. – Divya Jan 08 '18 at 09:43
  • Good work @Divya. – Alex Jan 08 '18 at 09:52
  • offcourse Divya, but iam tried then only iam asking, iam learning newly – user3544256 Jan 08 '18 at 09:56

1 Answers1

2

Let me know if you need to remove the comma as well.

var val = "2018-01-11,2018-01-03,2018-01-27,2018-01-31"
var searchstring = "2018-01-03"
var startindex = val.indexOf(searchstring)
var newVal = val.substr(0, startindex) + val.substr(startindex + searchstring.length, val.length)
console.log(newVal)

This will be the "cleaner" solution splitting your value to an array, then using Array.prototype.splice() to remove the unwanted date, then joining the array back to a comma-separated string.

var val = "2018-01-11,2018-01-03,2018-01-27,2018-01-31".split(',') // ["2018-01-11","2018-01-03","2018-01-27","2018-01-31"]
var search = "2018-01-03"
var index = val.indexOf(search)
if (index !== -1) {
  val.splice(index, 1)
  val = val.join(',')
}
console.log(val)
connexo
  • 53,704
  • 14
  • 91
  • 128