Lilz
Lilz

Reputation: 4081

html link to change the value of a textbox

I'm 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. 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>

Upvotes: 1

Views: 5138

Answers (2)

Canavar
Canavar

Reputation: 48088

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">

Upvotes: 0

Anonymous
Anonymous

Reputation: 50319

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.

Upvotes: 4

Related Questions