admin管理员组

文章数量:1313334

I've multiple buttons with related text to each button, when user click on the button, the text should change according to button and text should be displayed in the DIV.

I'm using if elseif to choose between the text for each button, now I'm unable to go through the function to pass the text to the div onclick().

<html>  
   <head>  
      function display (selected) {  
         if (decision == firstbox)  {  
            display = "the text related to first box should be displayed";  
         }  else if (decision == secondbox)  {  
            display = "Text related to 2nd box.";  
         }  else  {  
            display ="blank";  
         } 
   </head>  
   <body>  
      <input type="button" id="firstbox" value= "firstbox" onclick="display(firstbox)" /><br>    
      <input type="button" id="secondbox" value= "secondbox" onclick="display(firstbox)" /><br>
   </body>
</html>

I've multiple buttons with related text to each button, when user click on the button, the text should change according to button and text should be displayed in the DIV.

I'm using if elseif to choose between the text for each button, now I'm unable to go through the function to pass the text to the div onclick().

<html>  
   <head>  
      function display (selected) {  
         if (decision == firstbox)  {  
            display = "the text related to first box should be displayed";  
         }  else if (decision == secondbox)  {  
            display = "Text related to 2nd box.";  
         }  else  {  
            display ="blank";  
         } 
   </head>  
   <body>  
      <input type="button" id="firstbox" value= "firstbox" onclick="display(firstbox)" /><br>    
      <input type="button" id="secondbox" value= "secondbox" onclick="display(firstbox)" /><br>
   </body>
</html>
Share Improve this question edited May 27, 2013 at 2:02 demongolem 9,71836 gold badges97 silver badges105 bronze badges asked May 27, 2013 at 1:29 user2305807user2305807 211 gold badge1 silver badge3 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 2

Pure javascript from your code:

function display (selected)
  {
  if (selected == 'firstbox')
    {
    texttoshow = "the text related to first box should be displayed";
    }
  else if (selected == 'secondbox')
    {
    texttoshow = "Text related to 2nd box.";
    }
  document.getElementById("thetext").innerHTML = texttoshow;
  }

And the html:

<body>
  <div id = "thetext"></div>
  <button onclick = "display(firstbox)">Firstbox</button>
  <button onclick = "display(secondbox)">Secondbox</button>
</body>

For what is worth it, in jQuery (a javascript framework):

$("#buttonclicked").
  click(function(){
    $("#yourdiv").
      html("Your text");
    });

This should do what you want

<button type="button" id="button-test">Text that will apear on div</button>
<div id="content">
</div>
    <script type="text/javascript">
     $(document).ready(function(){
     $('#button-test').click(function(){
$('#content').text($(this).text());
    });
     });
    </script>

http://remysharp./2007/04/12/jquerys-this-demystified/

http://api.jquery./text/

Here I am going to provide you to examples in one. One of them use div and the other don't, one using a link and the other using a button that need to be clicked.

<!DOCTYPE html>
<html>
<body>

<h2>Displaying text when clicked</h2>

<button type="button"
onclick="document.getElementById('demo').innerHTML = 'These are the steps to get your PIN number: Bla bla bla'">
PIN button:</button>

<p id="demo"></p>


</br></br></br>

<a onclick="showText('text1')" href="javascript:void(0);">PIN link:</a>

<script language="JavaScript">
   function showText(id)
    {
        document.getElementById(id).style.display = "block";
    }
</script>

<div id="text1" style="display:none;">These are the steps to get your PIN number: Bla bla bla</div>

</body>
</html> 

本文标签: Display text in div when javascript onclickStack Overflow