Solved

Shorter JQuery

Posted on 2013-05-27
4
172 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 51

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 42

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
This article discusses how to create an extensible mechanism for linked drop downs.
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)

746 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

10 Experts available now in Live!

Get 1:1 Help Now