Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7940439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T23:23:20+00:00 2026-06-03T23:23:20+00:00

Original Question Is there a jQuery method that will check the selection type and

  • 0

Original Question

Is there a jQuery method that will check the selection type and set the value appropriately? Example, using .html() or .val().

I have made my own method to do the work for me however am not sure if this is the best way to preform this task.

$.fn.data = function(value) {
    if(this.is("input")){
        this.val(value);
    }else if(... other type ...){
        this.val(value);
    }else{
        this.html(value);
    }
};

I have also looked at using the tagName attribute.
var type = this.prop(“tagName”).toLowerCase();

Edit:

Benchmarking

For anyone interested I did a little benchmarking between this.tagName; and $(this).is(“input”); with 10,000 records.

this.tagName.toLowerCase(); x 10,000 records = 185 milliseconds

$(this).is("input"); x 10,000 records = 1676 milliseconds

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T23:23:21+00:00Added an answer on June 3, 2026 at 11:23 pm

    Here you go in plugin format:

    $.fn.setVal = function(value) {
    
        return this.each(function() {
    
            if ($.inArray( this.tagName.toLowerCase(), ['input', 'textarea', 'select'] ) != -1) {
                $(this).val( value );
            } else {
                $(this).text( value );
            }       
        });
    };
    

    Use it as follows:

    $('input, span').setVal('Some Value');
    

    See it here in action: http://jsfiddle.net/fkHLc/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Original Question: i read that for RESTful websites. it is not good to use
Edit: original question below, but I revise it now that I have some code
I'm currently using jquery ajax to call a pagemethod (which works great); $.ajax({ type:
I am using the jQuery validation plugin on my form. I set the basic
I am using jQuery resizable from interface.eyecon.ro/docs/resizable for a menu on a page that
I have noticed while monitoring/attempting to answer common jQuery questions, that there are certain
I'm using the jQuery UI Autocomplete. It's great, but there's a problem with multiple
There is a jQuery quiz posted on the W3Schools site here... http://www.w3schools.com/quiztest/quiztest.asp?qtest=jQuery Question #16
A few days ago I posted this question: switch statement and loops using jquery/javascript
See Edit below initial question for a revised version of the original problem Using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.