Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Shorter JQuery

Posted on 2013-05-27
4
Medium Priority
?
183 Views
Last Modified: 2013-05-28
How might I shorten /  improve / make more efficient this code to fill these two variables?

// the object
var wrkEvalType = $("#" + "YXZ").first()
// the value of the object
var wrkText = $("#" + "YXZ").first().text()

Help appreciated.

Thanks!
0
Comment
Question by:codequest
4 Comments
 
LVL 43

Assisted Solution

by:David S.
David S. earned 1200 total points
ID: 39200773
Like many jQuery methods, first() returns a jQuery object, so you can call the text() method on the object stored in the first variable instead of doing a second lookup operation for the element:
// the object
var wrkEvalType = $("#" + "YXZ").first();
// the value of the object
var wrkText = wrkEvalType.text();

Open in new window

0
 
LVL 60

Expert Comment

by:Julian Hansen
ID: 39200780
Not sure what you mean by the question.

You have assigned your object and value - how much shorter do you want it?
0
 
LVL 44

Accepted Solution

by:
Chris Stanyon earned 800 total points
ID: 39200795
Not sure why you're concatenating the selector - there seems to be no need. Also, because your selector is an ID, there should be no need for the first() function - you should only ever have one element using that ID. Finally, do you actually need to store the object in a variable or is that just to get the value.

//If you need the object AND the value in variables
var wrkEvalType = $("#YXZ");
var wrkText = wrkEvalType.text();

//If you only need the value
var wrkText = $("#YXZ").text();

Open in new window

0
 
LVL 2

Author Closing Comment

by:codequest
ID: 39201794
Thanks for the direction on this.   I appreciate in retrospect it wasn't that complicated;  I was apparently feeling pretty fuzzy about JQuery at the time I asked, and the answers provided clarity in several ways.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses
Course of the Month13 days, 1 hour left to enroll

972 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question