Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fill input text

Tags:

javascript

How can I fill a text input with a value? I am trying this:

<input id="curso" maxlength="150" name="curso" size="40" type="text" 
    value="window.location.href.substring(91))" />

But that does not work.

like image 724
Jean Avatar asked Sep 22 '11 19:09

Jean


People also ask

How do you fill text in HTML?

The fillText() method draws filled text on the canvas. The default color of the text is black. Tip: Use the font property to specify font and font size, and use the fillStyle property to render the text in another color/gradient.

How do you pre fill input fields?

# Use Prefill Form Fields for Your Popup CampaignsCopy a predefined smart tag and paste it into an Input Name field. For example, paste {{email}} into the selected input's name field. This will prefill that input.


1 Answers

<input id="curso" maxlength="150" name="curso" size="40" type="text">

<script>
document.getElementById("curso").value =
document.getElementById("curso").defaultValue = window.location.href.substring(91);
</script>

You can't execute JavaScript from within an arbitrary HTML attribute. Only <script> elements and event handlers can contain JavaScript, so add a <script> that sets the value.

You also need to set the defaultValue property so that any form reset resets the value to the desired value.

like image 77
Mike Samuel Avatar answered Sep 30 '22 18:09

Mike Samuel