Forum Moderators: open

Message Too Old, No Replies

JavaScript: refer back to name or value in condition

         

csdude55

8:28 pm on Nov 29, 2021 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I'm trying to clean up a repeated code format:

if (foo.length)
foo.val('bar');


I have similar code about a million times. In application, "foo" is really something larger and more complicated, $('input[name=' + restore[x] + ']') (in jQuery, obviously).

Is there something similar to "this" that can refer back to "foo" in the condition statement? I know how "this" works in a function, so I'm curious if there's something similar. This obviously doesn't work, but something like:

if (foo.length)
this.val('bar');


I know that I can create my own variable, like so:

var ph = $('input[name=' + restore[i] + ']');
if (ph.length)
ph.val('bar');


but before doing that a million times I want to make sure there's not a global variable that already has the same value.

NickMNS

1:20 am on Nov 30, 2021 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Based on your example:
foo.val(foo.length ? 'bar' : null)

but that doesn't really make sense, but using your real-life example it definitely works.

$("input[name=" + (restore.length ? null : restore[x] ) + "]"`)

If restore is not an empty list it returns the value of restore[x], otherwise null. The only problem in this case is if restore's length is 1 and x === 1, then it will return undefined. To handle that case you can use:
(restore[x] || null)

and ideally you should replace null with a default value, because in this case will return literally "null"

Also you can use template literals: `` with ${}
$(`input[name=${restore ? restore[x] : null}]"`)

[developer.mozilla.org...]
But this is not supported by IE.

Fotiman

11:54 pm on Nov 30, 2021 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Perhaps I don't understand exactly what you're asking, but it looks like you're checking whether a jQuery object contains at least 1 DOM element before attempting to set the value of all the DOM elements represented by the jQuery object? But there's really no need to check the length. You can just call foo.val('bar'); and if there are no DOM elements (ie - length == 0) then it's just does nothing.

Also, val can take a callback function which gets the index and the old value of the DOM element, and `this` within the callback is the DOM element:
[api.jquery.com...]

csdude55

1:48 am on Dec 1, 2021 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I didn't see anything about a callback, but I think you're referring to this?

$("input[type=text].tags").val(function(index, value) {
return value.trim();
});


My question was really more generic than that, though. I have a few scripts that are loaded on every page, and cumulative it's about 60kb... even after minimizing :-O I'm trying to shrink everything down to help them load faster, so I was hoping there might be a global variable similar to this (or $(this)) for a conditional statement that could shave a few microseconds off of the load time.

Fotiman

6:24 pm on Dec 1, 2021 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Correct, I was referring to that. The function passed into val is a callback function.

I was hoping there might be a global variable similar to this (or $(this)) for a conditional statement that could shave a few microseconds off of the load time.


Ok, I think I understand. The answer is no. Within a given conditional block, there are no globals (or global-like) variables/properties that give you access to anything in the conditional expression other than the variables you define.