PDA

View Full Version : jquery copy input to other input



nicmo
09-25-2010, 11:31 PM
Hello, i am trying to create a URL friendly address based on an input given by a user. This is with jquery.




<script type="text/javascript">
function suggestUrl() {

var title = ('twig_title');
title = title.replace(/\,/g, '-');
title = title.replace(/ /g, '-');
title = title.replace(/\@/g, '_at_');
title = title.replace(/[^A-Za-z0-9_\-]/g, '');
title = title.replace(/-+/g, '-');
title = title.replace(/-+$/g, '');
$('twig_name').value = title;

return true;
}
</script>


<form id="step1" name="step1" action="" method="post">

<input name="twig_title" type="text" id="twig_title" " onChange="showtwigtitle(this.value)">

<input type="text" name="twig_name" id="twig_name" onChange="showtwigname(this.value)">
</form>



I took this code from another site and cant seem to find what i am missing. Thanks for the help.

jscheuer1
09-26-2010, 02:51 AM
Your HTML markup has a syntax error:



<input name="twig_title" type="text" id="twig_title" " onChange="showtwigtitle(this.value)">

But that probably isn't the problem, definitely not the main problem. It should be fixed though. Simply remove it.

The main problem I see is that there are no:


showtwigtitle()

or:


showtwigname()

functions defined. It looks like these should both perhaps be:


suggestUrl()

I'll try that out and get back to you.

jscheuer1
09-26-2010, 03:10 AM
OK, this sort of works:


<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<script type="text/javascript">
function suggestUrl(val) {alert(val);

var title = val;
title = title.replace(/\,/g, '-');
title = title.replace(/ /g, '-');
title = title.replace(/\@/g, '_at_');
title = title.replace(/[^A-Za-z0-9_\-]/g, '');
title = title.replace(/-+/g, '-');
title = title.replace(/-+$/g, '');
$('#twig_name').val(title);

return true;
}
</script>


<form id="step1" name="step1" action="" method="post">

<input name="twig_title" type="text" id="twig_title" onChange="suggestUrl(this.value)">

<input type="text" name="twig_name" id="twig_name" onChange="suggestUrl(this.value)">
</form>

But I question the wisdom of the particular regExp's used with the replace commands, and am at a loss as to what the intended output is. If it is the intention to produce a syntactically valid URL, those regExp's need to be changed.

nicmo
09-26-2010, 09:54 AM
hi and thanks for the help. I am sorry i missed up my example, the functions showtwigtitle(this.value) and showtwigname(this.value) call something else (a title and name availability script). Also the extra " found there is because i deleted some code for posting here and clearly failed to do it well.

I need the field twig_title to be copied to the field twig_name while being regexed. I will then have a twig name and a friendly seo url to call it with php.

jscheuer1
09-26-2010, 01:52 PM
OK, so did this answer question?