1

I am trying to display a text when the user selects the input tag,

For example, I have the following code:

<tr> <td class="align"> Yes <br/> <input type="checkbox" /> </td> </tr> 

Once the user clicks on the check box, the following text will appear:

"If you have selected yes,....."

And I am not sure if JavaScript would be the best path to do this.

But, If it is, would it be something like:

<script> $(function(){ if(input was checked)//not sure what would go here, new to Javascript display results//not sure either, new to Javascript }); </script> 
1
  • 1
    Did you search anything as you are new in this ? Any references you found ? Commented Jul 1, 2016 at 19:12

7 Answers 7

2

If you can put the text after, you can do it in CSS.

Yes<br /> <input type="checkbox" /> <span class="message">You have said Yes</span> 
.message { display: none; } input[type=checkbox]:checked ~ .message { display: block; } 
Sign up to request clarification or add additional context in comments.

Comments

1

Using jQuery, To get the checkbox value, do something like this

$("input[type='checkbox']").val();

or you can get with class/id by

$('.chekboxClass').val(); $('#checkboxId').val();

Comments

1

no need JS,

you can achieve this with CSS only solution,

use the adjacent sibling selector + and :checked

.text { display: none } input:checked + .text { display: inline-block }
<table> <tr> <td> <span class="select">yes</span> <input type="checkbox" /> <span class="text">If you have selected yes,.....</span> </td> </tr> </table>

Comments

1

$("#checkbox").click(function() {	$("#clickedYes").toggle($(this).prop("checked")); });
#clickedYes { display: none; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> Yes <input id="checkbox" type="checkbox" /> <span id="clickedYes">If you have selected yes....</span>

Comments

1

js:

if($('input').attr('checked')) { $('#box').append("<div>If you have selected yes,.....</div>"); } 

html:

<tr> <td class="align" id="box"> <div>Yes</div> <input type="checkbox"/> </td> </tr> 

2 Comments

How would I whatever I want after the Yes
I have updated my answer @RobertoFlores. I hope this helps you. If you have any doubts you can ask me :)
1

You will want to hook up an onchange listener to the checkbox and have that call your function.

Here is some code snippet that will get you started.

var resultEle = document.getElementById("result"); function showAnswer(ele) { if(ele.checked) { result.innerHTML = "You checked Yes!"; } else { result.innerHTML = ""; } }
<table> <tr> <td class="align"> Yes<br /> <input type="checkbox" onchange="showAnswer(this)" /> </td> <td> <span id="result"></span> </td> </tr> </table>

EDIT: Looks like you are using JQuery, my answer is plain Javascript. I would strongly suggest you learn some vanilla javascript before using JQuery so that you can fully understand what is happening behind the scenes. MDN has a great starters guide! https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide

1 Comment

I love warcraft 4 O:
1

To expand Andrew Herder and dippas answers for future visitors, you can do multiples on the same page using id tags also:

 <input id="item1-checkbox" type="checkbox" />Mark Yes <span id="item1">You have said Yes</span> <input id="item2-checkbox" type="checkbox" />Mark Agree <span id="item2">You have said Agree</span> 

.

<style> #item1 { display: none } input#item1-checkbox:checked ~ #item1 { display: inline-block } #item2 { display: none } input#item2-checkbox ~ #item2 { display: inline-block } </style> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.