...

View Full Version : Help with FORM selection



jrp1
05-09-2009, 03:00 AM
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>

Thanks in advance =]

jrp1
05-09-2009, 03:55 AM
Any ideas?

bazz
05-09-2009, 04:11 AM
<div>
<img src="/images/file.png" name="image" onClick="sendText(document.search.q, 'FOO')">
</div>


at a guess, maybe change it as shown above?

bazz

jrp1
05-09-2009, 04:16 AM
Hahahah =p

No, I need both words to be inserted into the form via Javascript, but only the word "FOO" to be highlighted.

adios
05-09-2009, 04:20 AM
http://webcloud.se/article/Selecting_text_with_JavaScript/

jrp1
05-09-2009, 04:24 AM
Ahhh, yes, thats exactly what im looking for.

How do I make this work in my script though? I'm new to Javascript. Any help is appreciated, thanks =]

[EDIT] I tried implementing it into the script, but instead of sending the text to the form and highlighting "FOO", it submitted both words, "FOO BAR". How do I implement this into the sendText script above?

adios
05-09-2009, 05:33 AM
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>untitled</title>
<script type="text/javascript">

function createSelection(start, end, field) {

if ( field.createTextRange ) {

/*
IE calculates the end of selection range based from the starting point.
Other browsers will calculate end of selection from the beginning
of given text node.
*/

var newend = end - start;
var selRange = field.createTextRange();
selRange.collapse(true);
selRange.moveStart("character", start);
selRange.moveEnd("character", newend);
selRange.select();
}

/* For the other browsers */

else if( field.setSelectionRange ){

field.setSelectionRange(start, end);
}

field.focus();
}

function sendText(field, text, selection)
{
var start = text.indexOf(selection);
var end = start + selection.length;
field.value = text;
createSelection(start, end, field);
}


</script>
</head>
<body>
<img src="http://www.drinkprices.com/users/3/photos/466.jpg" width="200"
style="cursor:pointer;margin-bottom:12px;" onclick="sendText(document.search.q, 'FOO BAR', 'FOO')">
<form name="search">
<input name="q" type="text" />
</form>
</body>
</html>

Credit Webcloud if you use this.

jrp1
05-09-2009, 06:01 AM
Ahhh, you're awesome. lol@ the picture too btw.

Thanks for your help, and ill credit Webcloud.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum