Having trouble with Jquery slide toggle and append functionality?

I'm struggling to get this code right. Could it be a syntax issue? jQuery is confusing me, as the alert works but not the functions. Is there something specific I need to do in order to make them work properly? How can I ensure that everything runs smoothly?

$(document).ready(function() {
  $('#button1').click(function() {
    ('p').addClass('.red');
  });

  $('#button2').click(function() {
    $('img').slideToggle;
  });
  
  $()
});
body * {
  outline: 1px dotted palevioletred;
  font-family: Arial, Helvetica, sans-serif;
}

.container {
  width: 1024px;
  min-height: 700px;
}

.row {
  border-bottom: 3px solid black;
}

.click {
  display: inline-block;
  width: 200px;
  text-align: center;
  vertical-align: center;
  margin-bottom: 30px;
}

.content {
  display: inline-block;
  text-align: center;
  width: auto;
  margin: 25px;
}

button {
  height: 35px;
  width: 75px;
  text-align: center;
}

.red {
  background-color: red;
}

.blue {
  color: blue;
}
<script type="text/javascript" src='http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js'></script>

<div class="container">
  <div class="top_row">
    <div class="top_left click">
      <button id="button1">Add Class</button>
    </div>
    <div class="top_right content">
      <h1>Add Class</h1>
      <p class="blue">Click the button to add class 'red' to this paragraph.</p>
    </div>
  </div>
  <div class="middle_row">
    <div class="middle_left click">
      <button id="button2">Sidetoggle</button>
    </div>
    <div class="middle_right content">
      <h1>SideToggle</h1>
      <p>Click the button to slidetoggle a hidden image.</p>
      <img src="bayBridge.jpg">
    </div>
  </div>
  <div class="bottom_row">
    <div class="bottom_left click">
      <button id="button3">Append</button>
    </div>
    <div class="bottom_right content">
      <h1>Append</h1>
      <p>Click the button to append a new paragraph.</p>
    </div>
  </div>
</div>

Answer №1

Error occurred here : ('p').addClass('.red');

If you wish to select all <p> tags, the correct syntax is : $('p') (Refer to documentation HERE)

To add a class using addClass, check the documentation HERE where you only need to specify your CSS class without using the . (which is used for selecting by class like $('.className'))

Therefore, update ('p').addClass('.red'); to $('p').addClass('red'); and all your <p> elements will turn red when the button is clicked

$(document).ready(function() {
  $('#button1').click(function() {
    $('p').addClass('red');
  });

  $('#randomButton').click(function() {
    $('img').slideToggle;
  });
  
  $()
});
body * {
  outline: 1px dotted palevioletred;
  font-family: Arial, Helvetica, sans-serif;
}

.container {
  width: 1024px;
  min-height: 700px;
}

.row {
  border-bottom: 3px solid black;
}

.click {
  display: inline-block;
  width: 200px;
  text-align: center;
  vertical-align: center;
  margin-bottom: 30px;
}

.content {
  display: inline-block;
  text-align: center;
  width: auto;
  margin: 25px;
}

button {
  height: 35px;
  width: 75px;
  text-align: center;
}

.red {
  background-color: red;
}

.blue {
  color: blue;
}
<script type="text/javascript" src='http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js'></script>

<div class="container">
  <div class="top_row">
    <div class="top_left click">
      <button id="button1">Add Class</button>
    </div>
    <div class="top_right content">
      <h1>Add Class</h1>
      <p class="blue">Click the button to add class 'red' to this paragraph.</p>
    </div>
  </div>
  <div class="middle_row">
    <div class="middle_left click">
      <button id="randomButton">Slide Toggle</button>
    </div>
    <div class="middle_right content">
      <h1>Slide Toggle</h1>
      <p>Click the button to toggle a hidden image.</p>
      <img src="bayBridge.jpg">
    </div>
  </div>
  <div class="bottom_row">
    <div class="bottom_left click">
      <button id="button3">Append</button>
    </div>
    <div class="bottom_right content">
      <h1>Append</h1>
      <p>Click the button to append a new paragraph.</p>
    </div>
  </div>
</div>

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

Utilizing pure CSS to target the first and last classes

I want to use pure CSS to select only the first and last classes throughout the entire document. In my scenario, the structure looks like this: <div class="Container"> <div> <div class="Parent"></div> <pre> ...

Retrieving the information verified in the database

public function actionEditmul($id) { $sql1="SELECT * FROM category_product INNER JOIN category ON category_product.cat_id=category.cat_id WHERE category_product.product_id=$id"; $editcat=Yii::$app->db->createCommand($sql1)->quer ...

What is the correct location for me to store a text file so that AJAX can access it successfully?

After diving into a textbook to learn AJAX, I encountered an introductory exercise that required running some code. However, on line 16, there seems to be an issue with a GET request to retrieve a .txt file. Where exactly should I place the text file - i ...

Automatically adjust multiple images on an HTML webpage

Just starting out with html programming here. Looking to add top margin using a span class, any tips on how to tackle this issue? ...

Expanding your knowledge of AngularJS: utilizing ng-click to interact with elements

After a user clicks on an element in my app, I have some logic that runs. For example: html <h3 ng-click="showAlert('text')">this is text! (try to click and select)</h3> js $scope.showAlert = function(text) { console.log(' ...

Scoped Styles rarely stand a chance against more dominant styles

I'm facing a scope issue while learning Vue. Question: I am trying to make sure that the styles in profile.vue do not override the ones in sidebar.vue. I want the sidebar to have a red background and the profile section to be blue. Shouldn't usi ...

Update the content retrieved through ajax periodically

Looking to set up a periodic update for this ajax fetch function, say every 10 seconds. I've tried a few approaches but none seem to work as expected. That's why I'm reaching out here for help. So, any idea how I can refresh the content ev ...

Guide to making a dropdown menu that pulls information from a text box and displays the location within the text

I'm currently working on a unique dropdown feature that functions like a regular text field, displaying the text position. To achieve this, I've constructed a hidden dropdown menu positioned beneath the text field. My goal is to develop a jQuery ...

What's causing my struggle to override the bootstrap nav-link class in next.js?

I am facing an issue where I need to customize the active state of bootstrap .nav-link in the code snippet below: <li className="nav-item"> <a className={`${styles.navLink} nav-link`} role="tab" data-to ...

Getting the return value in JSP using JSP Ajax Servlet

When I click on one of the tables, another table is loaded using Ajax in my JSP: $("#tablesorter-demo tr").click(function(){ $('#tablesorter-demo tr').not(this).removeClass('hilite'); $(this).toggleClass(&apo ...

Sending multiple forms at once with a single submission

I've been racking my brain trying to determine the feasibility of a particular task. I am currently utilizing zen-cart as my shopping cart software, but what I really want to achieve is creating a hardcoded page featuring a list of 7-9 products. Each ...

What is causing the Jquery repeater to not trigger .keyup and .change events on items beyond the second one?

I noticed that the .keyup and .change events are only triggered in the first input field and not after I add a new item. Is there a way to make these events trigger when adding a new field? http://jsfiddle.net/q8pcoaxf/ $('.field').on(& ...

Scrollbar width does not expand when hovered over

I am having trouble increasing the width of the scrollbar when hovering over it. However, I keep receiving an error in the console stating that 'scrollbar.addEventListener' is not a function. JAVASCRIPT setTimeout(function() { const scrollbar ...

Creating resizable SVG elements using HTML or CSS for dynamic width and height

Is there a way to give my SVG element dynamic width and height in order to display the entire SVG image? For example, take a look at this CodePen link. <svg width="250" height="250" viewBox="0 0 250 250"> Alternatively, .svg { width : 250px; ...

Stopping videos in the JQuery Cycle Plugin triggers the stop event

Currently, I have a simple cycle set up with YouTube videos using the following code: <script type="text/javascript"> $(document).ready(function () { $('#cycle').cycle({ fx: 'fade', sp ...

Displaying AJAX response with AngularJS

My Angular script structure is shown below: var myapp = angular.module("Demo",["ngRoute"]) .config(function($routeProvider){ $routeProvider .when ...

The input box fails to show larger values on the user's page

Show me the biggest number that the user enters in an input box and then display it back to them. I need some improvements to my code, ideally making it possible with just one input box instead of two. <!DOCTYPE html> <html la ...

Trying to figure out how to execute a codeigniter helper function with JQuery AJAX?

I created a handy function in my helper file within codeigniter that formats prices based on the value and currency ID provided. if(!function_exists('format_price')){ function format_price($val,$curency_id=NULL){ $CI ...

Ways to prevent users from manually inputting dates in date fields

I am currently developing an application and I need to prevent users from manually entering a date in the type=date field on the HTML page. I want to restrict users to only be able to select a date from the calendar display, which is in the format MM/DD/YY ...

Show a picture on top of another picture

Is there a way to overlay an image and text on top of another image, similar to what is done on this website ? ...