tags:

views:

43

answers:

3
<form id="form1" method = "post">
Text1:<input type ="text" id="textname1"/><br>
<input type ="button" name="button2" id="button2" value="UPDATE">
</form>

<script type ="text/javascript">
    $(document).ready(function() {
        $("#button2").click(function(e){
        alert($("#textname1").attr('value').replace('-',''));
            });
        $( "#textname1" ).datepicker();
        $( "#textname1" ).datepicker("option", "dateFormat", 'yy-mm-dd' );

    });
</script>

Suppose if i enter the date in the field 2010-07-06 .When i click the button2 i get the alert as 201007-06.How can i replace the last hyphen(-)

+3  A: 

Change your replace function's regular expression argument to include the g flag, which means "global". This will replace every occurrence rather than just the first one.

$("#textname1").attr('value').replace(/-/g,'')
Vivin Paliath
when i replaced iam stilling getting the date as '2010-07-07'.I want to replace the hyphen
Someone
@Someone: You have to remove the quotes from the regex: `.replace(/-/g,''))`
Felix Kling
@someone Try with the correct example
Justin Johnson
@Justin, thanks for fixing it! I was in Java land where you need quotes around regular expressions. :p
Vivin Paliath
+1  A: 

1) replace it twice :))))

2) Use regexp syntax: str.replace(/-/g,"")

Sergey Osypchuk
A: 

You need to use a global regular expression, the regular expression is between /'s and g at the end means global so in your case:

"2010-07-06".replace(/-/g,'')

would remove all the dashes. So your code becomes:

$(document).ready(
 function() {
   $("#button2").click(function(e){
      alert($("#textname1").attr('value').replace(/-/g,''));
   });
   $( "#textname1" ).datepicker();
   $( "#textname1" ).datepicker("option", "dateFormat", 'yy-mm-dd' );
});
Adam