Jibi Abraham
Jibi Abraham

Reputation: 4696

Javascript syntactic sugar for object value setter

Is there an easy way to do this in one line

( node.innerText || node.textContent ) = text
// node = DOM node  
// text = string

Obviously the above does not work.

Upvotes: 1

Views: 130

Answers (4)

Leonid
Leonid

Reputation: 3171

One could do it like this:

node['textContent' in node? 'textContent' : 'innerText'] = text

Upvotes: 2

yogs
yogs

Reputation: 788

Use JQuery to shorten your syntax

use .text() method

so your code will be

if($(selector).text() = text){
// your code here
}

Upvotes: 0

user1726343
user1726343

Reputation:

Just use node.innerText = node.textContent = text, which is harmless.

Upvotes: 2

WTK
WTK

Reputation: 16961

First, not-so-compact attempt

node.innerText ? node.innerText = text : node.textContent = text

Upvotes: 1

Related Questions