View Full Version : sendText Help
jrp1
May 8th 2009, 4:06 pm
Im working on a script where, when you click an image, it automatically enters the text "FOO BAR" into a form field. How can I make it so just "FOO" is highlighted when this text is entered? Here's my current code:
<div>
<img src="/images/file.png" name="image" onClick="sendText(document.search.q, 'FOO BAR')">
</div>
jrp1
May 8th 2009, 8:20 pm
To elaborate, I need both words to be inserted into the form via Javascript, but only the word "FOO" to be highlighted.
hiteklife
May 10th 2009, 1:23 am
Hey, I think I have what you're after. Try this:
<script>
function sendText(field, content){
var content1 = content.substr(0, content.indexOf(" "));
document.getElementById(field).value = content;
document.getElementById(field).setSelectionRange(0, content1.length);
document.getElementById(field).focus();
}
</script>
<input type="text" id="search">
<img src="someImage.jpg" onclick="sendText('search', 'FOO BAR')">
Does that help?
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.