copying one field to another but adding commas between words?
I have a field called title.
I have a second field called tagline.
The user will fill in the title manually, and I would need the tagline field to be a copy of the title field, but with commas between each word.
So, a user would put "red apples" in the title, then the tagline would be the same as the title but with commas between the words.
Title = red apples
tagline = red, applesCan somebody t开发者_Go百科alk me through the code to do this please?
Thanks
var title = "red apples";
var tagline = title.split(" ").join(", ");
$title = 'red apples ';
$title= trim($title); //make sure no space before or after the string, it would convert to comma
$tagline = str_replace(' ', ',', $title)
Here is a working example
<input id="title" type="text" value="Red Apples" />
<input id="tagline" type="text" value="" />
<input type="button" value="do stuff" onClick="stuff()"/>
<script type="text/javascript">
window.stuff = function(){
title = document.getElementById("title");
tagline = document.getElementById("tagline");
tagline.value = title.value.split(" ").join(", ");
}
</script>
精彩评论