Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
$(document).ready(function(){
$('#channelType').on('change', function() {
if ( this.value == '1')
{
$("#internal").show();
}
else
{
$("#internal").hide();
}
});
});

What I have tried:

i tried to change the above jquery snippet into typescript
Posted
Updated 13-Jan-23 2:39am

1 solution

There are several problems with this code, first off that you are mixing jQuery with JavaScript—the context I am talking about here. For example, see here,
JavaScript
$('#channelType').on('change', function() {
   if (this.value == '1')
   { // ...

this refers to a JavaScript object, not a jQuery one, to refer to that you need to use $(this) object. So I can solve this for you to work in jQuery, provided everything else is fine.
JavaScript
$(document).ready(function(){
   $('#channelType').on('change', function() {
      if ($(this).val() == '1')
      {
          $("#internal").show();
      }
      else
      {
          $("#internal").hide();
      }
   });
});

This should work as jQuery, in case you wanted to make it work. If not, then you need to change a lot of things, starting with the HTML, perhaps your element is a select element, thus that goes like,
HTML
<select id="channelType" onchange="channelChanged();">
   <!-- Internal content -->
</select>

Then, somewhere in the JavaScript, you would write the following function to handle the change of value in that element,
JavaScript
function channelChanged() {
    // We can also pass this to the function
    var channelType = document.getElementById("channelType");
    var internalObject = document.getElementById("internal");

    // Change the display of the element.
    if(channelType.value == '1') {
        internalObject.style.display = 'block';
    } else {
        internalObject.style.display = 'none';
    }
}

But again, is this what you want to use? :-)

Read more about onchange event, onchange - Mozilla | MDN[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900