5

I have a new query on datepicker. I want to replace forward slash with comma (or dot) from date which is pick from datepicker. I tried some code below but it's not work fine.

Fiddle Here

HTML

<input type='text' id='txtDate' readonly='true' />
<input type='button' id='btnConvert' value='Change' /><br/>
Current Date : <span id='spnCurrentDate'></span>

Js

$("#txtDate").datepicker({
    changeMonth: true
});

$("#btnConvert").click(function(){
$("#spnCurrentDate").html($('#txtDate').val().replace('/', '.'));
});
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
S. S. Rawat
  • 5,943
  • 4
  • 43
  • 59

2 Answers2

3

Here

use regular expression. THis will work for you

$("#txtDate").datepicker({
    changeMonth: true
});

$("#btnConvert").click(function(){
$("#spnCurrentDate").html($('#txtDate').val().replace(/\//g, ""));
});
Deep Sharma
  • 3,374
  • 3
  • 29
  • 49
2

You need to escape the slashes. Try this:

$("#spnCurrentDate").html($('#txtDate').val().replace(/\//g, "."));

Working Demo

Milind Anantwar
  • 81,290
  • 25
  • 94
  • 125
  • 4
    Don't encourage users to ask questions that have clear duplicates, by answering them. – Cerbrus Jun 25 '14 at 12:57
  • 2
    @Cerbrus: It is not duplicate question it is different from that question. – S. S. Rawat Jun 25 '14 at 12:59
  • @Subhash: The problem in your question is that only the first occurrence of `'/'` got replaced. That's what the linked question is about, and what it answers. – Cerbrus Jun 25 '14 at 13:01
  • @Cerbrus even i can't answer after you marked it as a duplicate so i post it as a comment. – Just code Jun 25 '14 at 13:03
  • @Justcode: Maybe you shouldn't answer it, because it's already answered in the question this is a duplicate of. That's the entire point of dupe votes. There have been way too many questions about replacing all occurrences in a string, already. – Cerbrus Jun 25 '14 at 13:05
  • @Cerbrus that's fine i accept your notice :) – Just code Jun 25 '14 at 13:06