-
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
-
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.
-
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
-
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>
-
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
-
Re: concatenate string
Have you changed the target to _main?? I chnged it to _blank to it would work for me ;)
-
Re: concatenate string
of course.
i kept it on _main
-
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.
-
Re: concatenate string
ohh it's work
thanks a lot :)