PDA

Click to See Complete Forum and Search --> : concatenate string


shucus
Aug 14th, 2005, 07:09 AM
Hi,

i add a textbox and
i add an href that go like this:

<a href="http://www.google.co.il/search?q= target=_main"> click here </a>


no when the user click the link, i want it to go the address that in the href when the string in the text box concatenate after the ?q=
for example if the user wrote ski so the string go like this:
http://www.google.co.il/search?q=ski

how can i do that
thanks a lot

visualAd
Aug 14th, 2005, 12:18 PM
You would need to use Javascript to do this. Use the onclick event to intercept the mouse click and modify the element:

<html>
<body>
<script type="text/javascript">
function anchorClick(element)
{
var txtBox = document.getElementById('concat');

element.setAttribute('href', element.getAttribute('href') + txtBox.value);

}
</script>
<a href="http://www.google.co.il/search?q=" onclick="anchorClick(this)" target="_main"> click here </a>
<input type="text" id="concat" />

</body>
</html>

Notice the use of the id attribute for the text box. This is important as it allows you to refer to the element in the javascript code.

shucus
Aug 14th, 2005, 03:59 PM
it's work but when i click the link again it concatenate to the last string that was in the textbox.
for example if one time the user wrote ski and in the secend time the user wrote soccer. the secend time string will be skisoccer.

how can i get only the present string and not concatenate past string.

thanks a lot

visualAd
Aug 14th, 2005, 04:22 PM
Sorry about that, I've changed it so it uses a regular expression. The replace functions simply searchfor a q= in the string and replacesit with the text form the text box:

<html>
<body>
<script type="text/javascript">
function anchorClick(element)
{
var txtBox = document.getElementById('concat');
var regExp = /q=.*/;
var href = element.getAttribute('href');

href = href.replace(regExp, 'q=' + txtBox.value);

element.setAttribute('href', href);

}
</script>
<a href="http://www.google.co.il/search?q=" onclick="anchorClick(this)" target="_main"> click here </a>
<input type="text" id="concat" />

</body>
</html>

shucus
Aug 14th, 2005, 05:03 PM
it doesnt work

the first code work but keep the last string befor. the update code doesnt work. it's not move to any URL

visualAd
Aug 15th, 2005, 12:42 AM
Have you changed the target to _main?? I chnged it to _blank to it would work for me ;)

shucus
Aug 15th, 2005, 01:38 AM
of course.
i kept it on _main

visualAd
Aug 15th, 2005, 02:52 AM
I'm not sure why it doesn't work for you. I have tested this in Internet Explorer and Firefox. What browser are you using and what version. Also, turn on error notifications for Javascript and see if you get any errors.

shucus
Aug 15th, 2005, 06:28 AM
ohh it's work

thanks a lot :)