Compacting JQuery code

Posted on 2009-02-19
Last Modified: 2012-05-06
I don't know if this is possible, but I wonder if the following code can be put in one line (using sequencing) or one nested command. It works like this, but I'm just curious and want to understand JQuery better.
var title= $("#textfield").val();

$("#item td.first").html(title);

Open in new window

Question by:R7AF
    LVL 49

    Accepted Solution

    Not sure why you would want to compact it, but this is the only thing I could think of:

    $("#item td.first").html($("#textfield").val());

    You might however want to reduce the selector to:
    $("#item td.first").html(document.getElementById('textfield').value);

    LVL 13

    Author Closing Comment

    Thank you! I tried something similar, but didn't get it to work. I'm just starting with JQuery, so the "why" is more about understanding the different ways you can do things.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    by Julian Matz As of jQuery ( 1.4 the .live() method ( supports custom events as well as some standard JavaScript events that it previously didn't support. Among these is the submit event. Unlike …
    Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question ( on how to make a page show some balloons animate up a page…
    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…

    779 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

    14 Experts available now in Live!

    Get 1:1 Help Now