Solved

Shorter JQuery

Posted on 2013-05-27
4
173 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 42

Assisted Solution

by:David S.
David S. earned 300 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 53

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 43

Accepted Solution

by:
Chris Stanyon earned 200 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

DOM Attributes and Properties treatment with jQuery 1.6 by Ivo Stoykov jQuery (http://jquery.com/) 1.6 introduces .prop() (http://api.jquery.com/prop/) and .removeProp() (http://api.jquery.com/removeProp/) methods which allow modifying or removi…
This article discusses how to create an extensible mechanism for linked drop downs.
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)
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

864 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now