views:

379

answers:

2

Hi,

I am trying to create text in html, that once clicked, the the value of a textbox residing near it, changes its value to be equal to that of the text clicked.

Is there anyway I can do that?

In other words I want the functionality of a button (the onClick event) for a link/text.

Thank you

For example: What's wrong with this?

<td><input type="submit" name="submit" value=<%=text.toString()%>  onClick="(<% TextBox1.Text=text.toString()%>)" style="background:none;border:0;color:#ff0000"></td>

?

thx

+3  A: 

Assuming the input has an id attribute with value "foo":

onclick="document.getElementById('foo').value='bar';"

Frameworks like JQuery can make this slightly simpler:

$('#foo').attr('value', 'bar');

One could add this event to a span element, for example. It's best practice to use script to set these events and special styles to reflect how they act, so that without script their different (lack of) behavior still looks intuitive.

Anonymous
+1 especially for the degradation comment
Rob
A: 

You can do it like that :

<span onclick="document.getElementById('myTextbox').value=this.innerHTML;">text</span>
<input type="text" value="" id="myTextbox">

With JQuery :

<span onclick="$('#myTextbox').val(this.innerHTML);">text</span>
<input type="text" value="" id="myTextbox">
Canavar