JavaScript JavaScript and the DOM Making Changes to the DOM Getting and Setting Text with textContent and innerHTML

trying same javascript function with JQuerry

// javascript code

const input = document.querySelector('input'); const button = document.querySelector('button'); const p = document.querySelector('p.description');

button.addEventListener('click', () => {

p.textContent = input.value + ':';

});

// jQuery code

$('button').click(function(){

var v = $('input').val();
if(v == ''){
alert("Enter Some Text In Input Field");
}else{
alert(v);

} $('p.description').html() = v.val();

});

1 Answer

Steven Parker
Steven Parker
177,727 Points

Assuming that jQuery is being properly loaded, the problem is this line:

  $('p.description').html() = v.val();

You can't assign something to a method call, but the way the "html" method works is that you give it the value you want used as an argument inside the parentheses. Also, the variable "v" represents the value and not the element, so it has no "val" method. Finally, if you want it to do the same job as the plain JavaScript code, you'll need to append a colon to that value:

  $("p.description").html(v + ":");

And when sharing code on the forum, use the instructions for code formatting in the Markdown Cheatsheet pop-up below the "Add an Answer" area. :arrow_heading_down:   Or watch this video on code formatting.