Solved

Shorter JQuery

Posted on 2013-05-27
4
178 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 58

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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…
What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
The viewer will learn how to dynamically set the form action using jQuery.
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…

622 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