I have written code in JavaScript, but now I am interested in converting it to jQuery

What is the best way to convert this to jQuery?

showDish("balkandish1")
function showDish(dishName) {
    var i;
    $(".city").css('display', 'none');
    $("#" + dishName).css('display', 'block');
}

Answer №1

To hide and show specific elements, you can utilize the hide() and show() methods with the appropriate selectors as shown below:

displayMeal("italiandish1")

function displayMeal(mealName) {
    $(".location").hide()
    $('#' + mealName).show();
}

Answer №2

To display and hide elements, utilize the show() and hide() methods.

openSection("section1");

function openSection(sectionName) {
   // Select all elements with a specific class and hide them
   $(".content").hide();
   // Target a specific element by its ID and make it visible
   $('#' + sectionName).show();
}

openSection("section1");

function openSection(sectionName) {
  $(".content").hide();
  $('#' + sectionName).show();
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="content">Content 1</div>
<div class="content">Content 2</div>
<div class="content">Content 3</div>
<div class="content" id="section1">Content 4</div>
<div class="content">Content 5</div>
<div class="content">Content 6</div>

Answer №3

Utilizing jQuery's .show() and .hide() methods allows for the hiding and revealing of DOM elements.

revealRecipe("italiandish1");

function revealRecipe(recipeName) {
    $(".ingredients").hide();
    $('#'+ recipeName).show();
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The AngularJS beginner routing application is malfunctioning and needs fixing

I've been diving into Angular JS but hit a roadblock with a basic angular routing program. I need some guidance on what's going wrong. If you want to check out the complete project code, visit my GitHub repository: https://github.com/ashpratap00 ...

Using Redux saga: passing arguments to axios instance

I need to transition my code from thunk to saga to meet the requirements of my company. While it was easy to send api requests with params using thunk, I am struggling to figure out how to pass params to the axios request: redux/sagas/handlers/marketpla ...

Exploring the power of jQuery selector syntax with the inclusion of

There are multiple html structures similar to the one below: <div class="item"><p>...</p></div> I am looking to loop through them and select the p elements. I have tried different variations of the code below but couldn't get ...

The extent of the modal window (AngularJS directive)

I've been experimenting with a modal window feature using Angular UI Bootstrap. Within the parent controller, I've defined the following function: $scope.open = function () { var modalInstance = $modal.open({ templateUr ...

Which method of loading website images is quicker: sequential or parallel, utilizing Javascript?

As I work on developing an AJAX image gallery that preloads multiple large images ranging from 120kB to 2MB before the lightbox appears, a question arises: should these images be loaded sequentially (waiting for one to preload at a time) or in parallel? Wh ...

How to refresh an array in Angular 4 after inserting a new element using splice method?

I have a Angular list displayed in a table, and I need to insert an element at a specific position. I have tried using the following code: array.splice(index, 0, element); While everything seems fine in the console with the element being added at the corr ...

Is there a way to align this button perfectly with the textboxes?

https://i.sstatic.net/ODkgE.png Is there a way to align the left side of the button with the textboxes in my form? I'm using bootstrap 3 for this. Here is the HTML code for my form. I can provide the CSS if needed. Thanks. h1 { font-size:83px; ...

Update the dropdown choices using an ajax call with mobius1-selectr

Using selectr-master (https://github.com/Mobius1/Selectr) Hello, I'm not sure if I am implementing the correct code to achieve this task. I have 3 dropdowns that need to be populated using jQuery based on a selection made in another dropdown. I am at ...

A role requiring coordinates x, y, and z with significant values

http://jsfiddle.net/eho5g2go/ var offset=10000000; ... camera.position.set(offset,offset,400); mesh.position.set(offset-300,offset,0); camera.lookAt(mesh.position); ... animate(); The "offset" variable is crucial for determining the camera and mesh p ...

Clickable elements are not functioning on dynamically generated divs

In the process of developing an application using Angular, I encountered a scenario where I needed to fetch and display data from a web service. The challenge was in dynamically creating div elements with the retrieved data: for(var i = 0 ; i < data.Ou ...

Error in loading jquery for Mediawiki version 1.27.4

I am encountering an issue with Mediawiki and Resourceloader. I recently installed MediaWiki 1.27.4 LTS version and upon installation, I noticed that jquery, which is supposedly loaded by default, is not appearing in the sources tab of Chrome developer too ...

Arrangement using the display property of inline-block not following a linear direction

I'm experiencing some issues with the arrangement of elements on this page: When the viewport width is around 800px, the layout appears misaligned. Some lines have only a few bottles even though there is space for more. Is there a solution to this pr ...

Concealed Input Enhancements for AutoComplete

Hey there! I'm new to AJAX and could really use some help with my autocomplete function. I'm trying to store the ID of the user's selection in a hidden input field, but I've hit a roadblock. So far, my function isn't working as exp ...

Make sure that JSON.stringify is set to automatically encode the forward slash character as `/`

In my current project, I am developing a service using nodejs to replace an old system written in .NET. This new service exposes a JSON API, and one of the API calls returns a date. In the Microsoft date format for JSON, the timestamp is represented as 159 ...

Showing a div with 2 unique data attributes - such as displaying Currency and Quantity

My E-Commerce website is created using HTML, JavaScript, and PHP. On the product details page, users can add products to their cart, so I show the total cart value. I need the total amount displayed in a decimal number format (10,2). Currently, when a u ...

Upgrading the entire document's content using jQuery

I am dealing with an ajax response that provides the complete HTML structure of a webpage, as shown below: <!DOCTYPE> <html> <head> <!-- head content --> </head> <body> <!-- body content --> </b ...

What is the best way to organize an array inside a jQuery .each loop?

I am currently utilizing a .each loop to iterate through various div elements and extract the values from specific input boxes contained within them. Each div represents a row and is identified by an Id, such as plot1, plot2.... I aim to collect all this d ...

Ensure that a div with fluid width remains consistently centered within another div

I have a bunch of boxes filled with content. The number of boxes in each row varies depending on the width of the browser window. Is there a way to ensure that all the boxes are always horizontally centered on the page? For guidance, you can check out th ...

Create a distinct CSS animation 'origin' declaration for every <g> element

I am using CSS animation to apply a 'wobble' effect to each letter within a word. The letters are represented by SVG groups <g>. However, in the provided example, the wobbling effect becomes more pronounced with each subsequent letter, wher ...

Sending Profile Picture and Description to PHP using AJAX and jQuery

I have been struggling to upload user images via AJAX to a PHP database. Despite trying various tutorials and examples, nothing seems to work with my code. The codes function properly without AJAX, but I need the upload process to be seamless for users by ...