Call javascript function with an href - Stack Overflow

admin2025-04-19  0

i'm developing a website, but i'm not an expert when it es to javascript. I'm using a script that i found on the internet for searching the page for a text string. To call this function i use the following code:

  <div class="pesquisa" id="pesquisa"><form name="f1" action=""
  onSubmit="if(this.t1.value!=null && this.t1.value!='')
  findString(this.t1.value);return false">
  <input name="t1" type="text" /></form>
  </div>

this is working well, when i enter text and click enter he finds it on the page. Now i have a "button" next to it that is represented by:

<a href='javaScript:document.f1.findString(this.t1.value)'>
<div class="enviar" id="enviar">Pesquisar</div></a></div>

I want this "button" to call the same function with the value inserted in the form. But it is not working this way. Any ideas on how this can be made ?

Thanks in advance, Cláudio

i'm developing a website, but i'm not an expert when it es to javascript. I'm using a script that i found on the internet for searching the page for a text string. To call this function i use the following code:

  <div class="pesquisa" id="pesquisa"><form name="f1" action=""
  onSubmit="if(this.t1.value!=null && this.t1.value!='')
  findString(this.t1.value);return false">
  <input name="t1" type="text" /></form>
  </div>

this is working well, when i enter text and click enter he finds it on the page. Now i have a "button" next to it that is represented by:

<a href='javaScript:document.f1.findString(this.t1.value)'>
<div class="enviar" id="enviar">Pesquisar</div></a></div>

I want this "button" to call the same function with the value inserted in the form. But it is not working this way. Any ideas on how this can be made ?

Thanks in advance, Cláudio

Share asked Jul 2, 2011 at 13:40 Cláudio RibeiroCláudio Ribeiro 1,6993 gold badges42 silver badges66 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 1

First, add an id attribute to the textbox:

<input id="t1" name="t1" type="text" />

Then, in your <a> tag, you can do this:

<a href="#" onclick="findString(document.getElementById('t1').value); return false;">Click Me</a>

Try this.. Add an id attribute to t1 (also called "t1"), and then reference it like this:

<a onclick="javaScript:findString(document.getElementById('t1').value)">
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745071590a283314.html

最新回复(0)