0

When I use JavaScript it works.

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

When I use jQuery to declare it it doesn't.

var submitButton = $('#submitButton');

I realized when debugging, jQuery is creating a object for the variable.

submitButton: Object[span#submitButton.wordButtonD]

In JavaScript

submitButton: span#submitButton.wordButtonD

How do get this to be like javaScript??

6
  • What "it works" means? Commented Jan 7, 2015 at 2:11
  • Try this var submitButton = $("#submitButton").attr("id"); Commented Jan 7, 2015 at 2:11
  • Sorry I meant, it works as in when I use it as an event. Like submitButton.onclick Commented Jan 7, 2015 at 2:18
  • @user2537485: why don't you use jquery's .click() instead? $('#submitButton').click(function() { // your handler here }); Commented Jan 7, 2015 at 2:20
  • @zerkms: oh, thanks for letting me know :) I am just starting to learn jQuery, I was just confused why the variable set up didn't work. That does look like a better approach though. Commented Jan 7, 2015 at 2:24

3 Answers 3

1

I think what you are looking for is: submitButton[0]

Just remember to check the length (greater than 0) because jQuery will return a result even though the element has not been found.

Sign up to request clarification or add additional context in comments.

4 Comments

"because jQuery will return a result" --- it's not a jQuery, but javascript that will do
@zerkms I would not agree. JavaScript doesn't do anything on its own. It is the jQuery framework that returns a object so that you can chain your calls without worrying for null.
that's correct, but when you perform [0] it's JS that will return undefined since it's not possible (?) to override the indexer operator in JS.
@user2537485: if you work with jquery - why don't you use facilities it provides? It looks strange.
0

Just use Javascript and the result will be as you want. jQuery create its own object to control :). Or please explain more why do you need the result span#submitButton.wordButtonD but not Object[span#submitButton.wordButtonD]?

Comments

0

statement 1:

 var submitButton1 = document.getElementById("submitButton");

statement 2:

var submitButton2 = $('#submitButton');

In above statements, submitButton1 is a DOM object and submitButton2 is a Jquery Object (which already warped your DOM object). Thus, If you want to get Dom object from Jquery object, just do:

var submitButton3 = submitButton2[0]

OR

var submitButton3= submitButton2.get(0)

Now, 2 Dom object submitButton1 and submitButton3 are the same.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.