Debug School

rakesh kumar
rakesh kumar

Posted on

how to take html different tags or multiple tag at a time in jquery and javascript inside script

how to take html different tags in jquery inside script

To select HTML elements of different types using jQuery, you can use any valid CSS selector to target the elements you want to select.

<div>
  <p>Paragraph 1</p>
  <span>Span 1</span>
  <p>Paragraph 2</p>
  <span>Span 2</span>
</div>
Enter fullscreen mode Exit fullscreen mode

jQuery

// Select all paragraphs and spans inside the div
var myElements = $('div p, div span');

// Loop through the selected elements and output their contents to the console
myElements.each(function() {
  console.log($(this).html());
});
Enter fullscreen mode Exit fullscreen mode

In the above example, we use the CSS selector div p, div span to select all p and span elements inside the div element. We then use the .each() method to loop through the selected elements and output their contents to the console using console.log(). The $(this) keyword refers to the current element in the loop, which we then use to retrieve the element's HTML content using the .html() method. You can replace the selector with any other valid selector based on your HTML structure.

how to take html different tags in javascript inside script

To select HTML elements of different types using JavaScript, you can use any valid CSS selector to target the elements you want to select. Here's an example:

HTML:

<div>
  <p>Paragraph 1</p>
  <span>Span 1</span>
  <p>Paragraph 2</p>
  <span>Span 2</span>
</div>
Enter fullscreen mode Exit fullscreen mode

JavaScript:

// Select all paragraphs and spans inside the div
var myElements = document.querySelectorAll('div p, div span');

// Loop through the selected elements and output their contents to the console
myElements.forEach(function(element) {
  console.log(element.innerHTML);
});
Enter fullscreen mode Exit fullscreen mode

In the above example, we use the querySelectorAll method to select all p and span elements inside the div element using the CSS selector div p, div span. We then use the forEach method to loop through the selected elements and output their contents to the console using console.log(). The element parameter in the loop refers to the current element in the loop, which we then use to retrieve the element's HTML content using the .innerHTML property. You can replace the selector with any other valid selector based on your HTML structure. Note that unlike jQuery, which is a library built on top of JavaScript, the vanilla JavaScript method does not require you to load any external libraries.

Top comments (0)