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 7760225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:49:36+00:00 2026-06-01T13:49:36+00:00

I have the following: Javascript: $(.bg .thumb_wrapper).click(function() { $(.bg .thumb_wrapper).removeClass(active); $(this).addClass(active); }); So every

  • 0

I have the following:

enter image description here

Javascript:

$(".bg .thumb_wrapper").click(function() {
    $(".bg .thumb_wrapper").removeClass("active");
    $(this).addClass("active");
});

So every time I click on a new background, it is highlighted and is given a class “active” and the previous active class is removed.

Now all I need to do is create a variable and set it to the data-name attribute for that active item.

Here is the HTML:

<div class="thumb_wrapper active">
    <img src="images/backgrounds/thumb/bg2.jpg" data-name="bg2.jpg">
</div>

So if I put it in the click function, it will work fine:

var bg = $(".bg .thumb_wrapper.active img").attr("data-name");
console.log(bg);

so every time it is clicked, it tells me the value. But I need to access that variable outside of that function.. so it needs to be global.

Is there anything I can do to grab the value of data-name on the start of the page, not on a click function? And when I do click a new BG and the active class is updated, then the variable will be updated also?

Hope that makes sense.

Thanks!

  • 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-01T13:49:37+00:00Added an answer on June 1, 2026 at 1:49 pm

    Declare the variable outside the function so its scope is not limited to the click handler.

    var bg;
    
    $(".bg .thumb_wrapper").click(function() {
       $(".bg .thumb_wrapper").removeClass("active");
       $(this).addClass("active");
       bg = $(".bg .thumb_wrapper.active img").data("name");
    });
    
    console.log(bg); //accessible outside
    

    If you want the value of bg to be updated everytime a new BG is clicked, you must do the assignment in the click handler as above.

    Live Example

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

Sidebar

Related Questions

I have the following JavaScript (with jQuery) $(function() { $(.btnDesc).click(function() { $(#desc).slideToggle(slow); $(this).toggleClass(active); });
I have the following javascript function: <script type=text/javascript> function quickCardRegister_OnCompleteSave() { publishContent('This is a
I have the following Javascript code add_num = { f: function(html, num) { alert(this.page);
I have following Javascript objects: function Alfa() { this.status=''; this.setStatus = function(value) { this.status=value
I have this following javascript to activate sometime document.addEventListener('touchmove', function (e) { e.preventDefault(); },
I have the following gallery set up in jQuery: - <script type=text/javascript> $(function ()
I have following JavaScript function to make some calculation with textboxes but when I
have the following javascript code // note: declaring i in this loop for( var
I have following javascript class: var ImageGallery = function(arr) { var ImgArr = arr;
I have the following Javascript function that makes a call to an Asp.Net WebMethod

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.