admin管理员组

文章数量:1390334

I want to type in text in a text field, press a button, and the text of a paragraph will change. What would I need to do for this to happen and is there an easier way to do it other than javascript?

Since I didn't know what I needed to do, here's the code I originally had:

<html>
<head>
    <title>Moving Text</title>
    <link rel="stylesheet" type="text/css" href="stlye.css">
</head>
<body>
<div id="text">
    <p id="new">new text</p>
    Main News:<input type="text" name="update"><br>
    <input type="button" value="Update" onClick="update();">
<script type="text/javascript"> 
    function update(){
        document.getElementById('new').innerHTML = 'Update';
    } 
</script>           
</div>
</body>

I'm pretty sure it's wrong or way off. Any suggestions?

I want to type in text in a text field, press a button, and the text of a paragraph will change. What would I need to do for this to happen and is there an easier way to do it other than javascript?

Since I didn't know what I needed to do, here's the code I originally had:

<html>
<head>
    <title>Moving Text</title>
    <link rel="stylesheet" type="text/css" href="stlye.css">
</head>
<body>
<div id="text">
    <p id="new">new text</p>
    Main News:<input type="text" name="update"><br>
    <input type="button" value="Update" onClick="update();">
<script type="text/javascript"> 
    function update(){
        document.getElementById('new').innerHTML = 'Update';
    } 
</script>           
</div>
</body>

I'm pretty sure it's wrong or way off. Any suggestions?

Share Improve this question edited Apr 17, 2014 at 16:18 FreeFire asked Apr 17, 2014 at 16:12 FreeFireFreeFire 1691 silver badge10 bronze badges 1
  • You would have to attach an event listener to the button which triggers a function that takes the value of the text field and inserts it into the paragraph, and no, javascript is the easiest way. You should post code that you have already tried if you want more help – fiction Commented Apr 17, 2014 at 16:16
Add a ment  | 

2 Answers 2

Reset to default 4

HTML:

<p id="your_paragraph">This text will change, after clicking the button.</p>
Main News: <input type="text" id="theText" />
<input type="button" id="btn" value="Update" />

JavaScript:

var p = document.getElementById('your_paragraph');
var btn = document.getElementById('btn');
var txt = document.getElementById('theText');
btn.onclick = function(){
    p.textContent = txt.value;
};

http://jsfiddle/3uBKC/

No, you'll need to use javascript. Without an example of your markup nobody will be able to provide you a specific example, but here's a general one using the jQuery library.

// get the textarea, watch for change, paste, and keyup events
$('textarea').on('change paste keyup', function(){

    // Store the text field as a variable, get it's value
    var thiis = $(this),
        value = thiis.val();

    // replace the paragraph's content with the textrea's value
    $('p').html(value);
});

本文标签: htmlChanging a paragraph via a text field using javascriptStack Overflow