1

I did try to do this (with jQuery):

type = (blabla == blublu)? 'category':'regular';
(type+'Name').val('...');

this is the console response:

Uncaught TypeError: Object categoryName has no method 'val'

although they actually exist...

so how can I make it work??

edit:

this is the full function:

var type = (1 == 1)?'something':'blabla';

var somethingName = $('#somethingName');
var blablaName = $('#blablaName');
more variables like this..

function itemMode(mode, inputValue){
  inputValue = (typeof inputValue !== 'undefined')? inputValue:'';
  var functionName = null;
  switch(mode){
  case 'open':
    btnValue = 'something';
    functionName = editCategory; // this need to be from type to
    openItem = true;
    break;
  case 'close':
    btnValue = 'something';
    functionName = addCategory; // this need to be from type to
    openItem = false;
    break;
  default:
    return false;
  }
  (type+'Name').val(inputValue);
  $('#btn'+type.capitalize()).off('click').val(btnValue).click(functionName);
  (type+'Name').off('keypress').keypress(function(e){enterPress(e,functionName);});
}

i try (type+'Name') to crate reference to an existing var or object.

12
  • 1
    can you present your actual full jQuery code? And explain what exactly you want to get from where? Commented Nov 4, 2013 at 12:56
  • 1
    Assuming categoryName for example is an ID attribute, you would go $('#' + type + 'Name'). Commented Nov 4, 2013 at 12:58
  • Global variables are properties of the window object: window["foo"+"bar"] Commented Nov 4, 2013 at 12:59
  • Please clarify whether you want to deal with CSS selectors or Javascript variables. Commented Nov 4, 2013 at 13:12
  • ok i will edit the question... Commented Nov 4, 2013 at 13:15

5 Answers 5

8

Your problem is not with the concatenation. That works fine. You concatenate type with 'Name', and you get the string 'categoryName'. You then do this:

('categoryName').val(...);

This is the same as 'categoryName'.val(...), which obviously won't work, because strings do not have a val method.

You are attempting to do a jQuery selection, for which you need the jQuery constructor:

$(type+'Name').val('...');

This, of course, is also unlikely to work, unless you have elements with the tag name categoryName. Presumably you actually want elements with that class:

$('.' + type + 'Name')

or id:

$('#' + type + 'Name')

or something similar.

Sign up to request clarification or add additional context in comments.

1 Comment

ya i thought about it but ן'm trying to do a function that will fit some situations and this is not the only id thet influence form that function
2

Variables are created as properties of the window object, so if they are global you can do this...

window[type + "Name"]

Since you want to refer to a "cached" jQuery object, use the following...

(window[type + "Name"]).val("123");

For example, if you've cached a jQuery object like this...

var sumVar = $('#sumid');

You can refer to it directly like this...

sumVar.val("123");

Or you can refer to it using a variable as the name, like this...

var varName = "sumVar";

(window[varName]).val("123");

Here's a modified version of the script you posted above...

var type = (1 == 1)?'something':'blabla';

window.somethingName = $('#somethingName');
window.blablaName = $('#blablaName');

function itemMode(mode, inputValue){
    inputValue = (typeof inputValue !== 'undefined')? inputValue:'';
    var functionName = null;
    switch(mode){
        case 'open':
            btnValue = 'something';
            functionName = editCategory; // this need to be from type to
            openItem = true;
            break;
        case 'close':
            btnValue = 'something';
            functionName = addCategory; // this need to be from type to
            openItem = false;
            break;
        default:
            return false;
    }

    (window[type + 'Name']).val(inputValue);

    $('#btn' + type.capitalize()).off('click').val(btnValue).click(functionName);

    (window[type + 'Name']).off('keypress').keypress(function(e){enterPress(e,functionName);});
}

17 Comments

it just i dont want to do this $('#sumid') in the function because i did it 2 lins before the function declared so i did $('#sumId') into sumVar and when i do sumVar.jQuerystuff() it work but when i try to expand my function to more same suff i need it to be variable
I learned Programming not to repeat things twice, this is my goal
Okay, I've updated the answer and I think it will help more.
If you just want to refer to a cached jQuery object then just use the variable. var $body = $("body"); $body.hide(); - for example.
I try my best but i do var sumVar = 123 and console.log(sumVar); return 123 and console.log(window[sumVar]); return undefined
|
0

You can create a function and use it's returned value:

DEMO1       DEMO2

function type(){
  return (blabla == blublu) ? 'category' : 'regular';
}


$('[name="'+ type() +'Name"]').val('SOME VALUE'); 

If you use ID rather than name attribute:

function type(){
  return (blabla == blublu) ? '#category' : '#regular';
}


$( type() +'Name').val('SOME VALUE'); 

Or just simply:

var typeID =  blabla==blublu? '#category' : '#regular';
$( typeID +'Name').val('SOME VALUE'); 

Comments

0

val() is a method provided by jquery. So to access it you will need to use the '$' operator along with the mode of selection :

var type = (blabla == blublu)? 'category':'regular';

For accessing using ID :

$("#"+type).val("...");

For accessing using Class :

$("."+type).val("...");

For accessing using any custom attribute for eg. data :

$("data['"+type+"']").val("...");

Comments

-2

You only can safely do that with an object key.

Create an object like so.

var obj = {
   'categoryName': ...

};

To retrieve it use something like this:

obj[someString]

eval is an option is not wise.

1 Comment

Do what? I'm sorry but your answer is as muddled as the question :/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.