|
-
Aug 14th, 2005, 07:09 AM
#1
Thread Starter
Lively Member
concatenate string
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
-
Aug 14th, 2005, 12:18 PM
#2
Re: concatenate string
You would need to use Javascript to do this. Use the onclick event to intercept the mouse click and modify the element:
HTML Code:
<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.
-
Aug 14th, 2005, 03:59 PM
#3
Thread Starter
Lively Member
Re: concatenate string
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
-
Aug 14th, 2005, 04:22 PM
#4
Re: concatenate string
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 Code:
<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>
Last edited by visualAd; Aug 15th, 2005 at 12:43 AM.
-
Aug 14th, 2005, 05:03 PM
#5
Thread Starter
Lively Member
Re: concatenate string
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
-
Aug 15th, 2005, 12:42 AM
#6
Re: concatenate string
Have you changed the target to _main?? I chnged it to _blank to it would work for me
-
Aug 15th, 2005, 01:38 AM
#7
Thread Starter
Lively Member
Re: concatenate string
of course.
i kept it on _main
-
Aug 15th, 2005, 02:52 AM
#8
Re: concatenate string
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.
-
Aug 15th, 2005, 06:28 AM
#9
Thread Starter
Lively Member
Re: concatenate string
ohh it's work
thanks a lot
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|