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

  • Home
  • SEARCH
  • 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 8557025
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:28:54+00:00 2026-06-11T15:28:54+00:00

i made a fiddle with buttons. Now in the javascript, I’m trying to learn

  • 0

i made a fiddle with buttons. Now in the javascript, I’m trying to learn jquery and by doing so I’m converting old fiddles into jquery from javascript however I know how.

My problem is that in my function called init, I can’t figure out how to convert the javascript way of get an html element with an id stored in a variable.

Old code in javascript:

var but = document.getElementById("but");

New code in jQuery:

var but = $('#but');

I think the problem is that I start with a javascript statement but then use jQuery. I don’t know what to do in terms of variables in jQuery.

  • 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-11T15:28:55+00:00Added an answer on June 11, 2026 at 3:28 pm

    You need to add in [0] to your jquery code to get the document element, but that is rather pointless with the jquery method of adding event listeners. I would suggest either $('#but').mouseout(etc) or $('#but').on('mouseout', etc).

    I’ve updated your jsfiddle to work as expected, though I’ll attempt to give a short tut here:

    There are two methods of adding event listeners you should familiarize yourself with per the jquery documentation; the .on() method, and the .(event)() method. The latter you can add to jquery ojects in lieu of object.(eventName)() as an example, adding the click handler to an object: object.click(function() { console.log('executed'); });

    This method however is not ‘live’ it will not update itself if the elements are added dynamically, and the events are only attached when the document is ready($(document).ready(function() { do stuff });). In order to attach events to dynamically added elements, we need the .on() method.

    Take for example the following html:

    <div class="wrapper">
        <span class="dynamically_added">stuff</span>
    </div>
    

    In order to attach an event listener to the dynamically added span, in your jquery, add the following:

    $(".wrapper").on('click', '.dynamically_added', function() {
        console.log('executed');
    });
    

    The first parameter of .on() is(are) the event(s). You can attach multiple events by delimiting them with spaces: .on('click hover'). The second parameter is either the function to execute, or the targeted element. In the case of the above example it is the span. The last parameter is of course the function to execute. As far as I am aware, you need to have an anonymous function to refer to the function to execute, instead of simply writing it there.

    I hope this has helped.

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

Sidebar

Related Questions

I've made a fiddle from my problem http://jsfiddle.net/bYMGZ/7/ The first popup displays from pressing
I am using jQuery 1.7 and jQuery UI 1.8.16 I made a js fiddle
Ive made a fiddle of my problem here. http://jsfiddle.net/E9cUS/1/ JavaScript: $('.top').click(function () { var
I am using some code from a fiddle made by a member of StackOverflow.
I have made a fiddle: http://jsfiddle.net/csS24/ I'm trying to get the code to a
I have made a fiddle: http://jsfiddle.net/89x4d/ I'm trying to maintain the skewed div but
I am learning about Javascript prototypes and made a Fiddle ( http://jsfiddle.net/3MuZa/1/ ) with
I have made a Fiddle for reference: http://jsfiddle.net/xypQY/ Which element would i apply WebKit
I have made a quick fiddle: http://jsfiddle.net/tLLB4/ Ok, so the premise is, i'm hovering
I made an iphone app to capture image from camera and to set that

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.