Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Shorter JQuery

Posted on 2013-05-27
4
Medium Priority
?
181 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 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 59

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The most up-to-date version of this article is on my Blog https://iconoun.com/blog/
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
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…

715 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