Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8234683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T18:33:57+00:00 2026-06-07T18:33:57+00:00

I have a slight problem with code here. I’m fairly new to it so

  • 0

I have a slight problem with code here. I’m fairly new to it so if its simple I apologise. But would love an explanation.

So here is the html I have:

<div class="one" data-selection="1"></div>
<p class="selections"></p>
<p class="cost">£ <span class="price">0</span></p>

and here is the JavaScript:

<script>
// Empty array to capture selections
    var a = [];
    var originalValue = "You have Selected section(s): ";  
    $(".one").click(function () {
        if ($(this).attr('class') == "selected one") {  
            //alert
            alert("Are you sure you wish to de-select?");       
            //remove from the total
            $(".price").html(parseInt($(".price").html(),10) - 30)  
            $(this).removeClass("selected ");   
            // need to get remove from array
            var removeItem = $(this).attr("data-selection");  
            a = jQuery.grep(a, function(value) {
                return value != removeItem;
            });      
            $('.selections').text(originalValue + a);   
        }
        else {
            var selection = $(this).attr("data-selection");
            alert(selection);
            var originalSrc = $(this).attr('class');
            $(this).attr('class', 'selected ' + originalSrc);     
            a.push(1);
            a.sort();           
            $('.selections').text(originalValue + a);   
            $('.price').text(function(i, txt) {
                return +txt + 30;
            });
        }
    });
</script>

This works fine. However when I add the contents of the if statement into a function called:

undoSelection()

and change the code to:

$(".one").click(function () {
    if ($(this).attr('class') == "selected one") {
        undoSelection();
    }
    else {

Nothing after the alert asking if they are sure to deselect works. If I add an alert in there and get

  $(this).attr("data-selection"); 

it alerts undefined.

I would like it in a function as I dont want lines upon lines of duplicated code 🙁

Can someone please shed some light on this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T18:33:59+00:00Added an answer on June 7, 2026 at 6:33 pm

    That’s because you have $(this) being called still I bet. The keyword this means $(".one") initially, however the context of this then changes when you call the function. If you want it to work in another function you need to set a variable to the value of $(".one") and use that instead of $(this) inside the function.

    It’s hard to get but the this keyword in javascript is always refers to the object that a function is a method of. So in the first case this refers to $(".one") whereas if you move the the other logic inside another function the definition changes (to the global object, usually the window).

    I’ve created a jsFiddle for you that shows a working example of what you’re looking for:
    http://jsfiddle.net/CKCjz/1/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

am new here. i have a slight problem; PLease look at the following code
I've wrote this simple piece of code. And I have a slight problem with
I have a slight problem my code works fine but it re-sizes my entire
hey everyone, here is the site SEE BELOW I have a slight jquery problem
I have a slight problem here I try to solve. As I start to
I'm relatively new at PHP and came across a slight problem. I have a
Having a slight problem on C#, still quite new to the language but hoping
I have a slight problem with creating a button on the server side code.
I have a slight problem implementing vibration functionality in my application. My code is:
I have a slight algorithmic problem. I think I miss something but can't exactly

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.