Jump to content

Longstring shortened down


Irresistable

Recommended Posts

var deletelink = document.createElement('a');
var t = document.createTextNode("X");
deletelink.setAttribute('href', '#');
deletelink.setAttribute('style', 'float:right;');
new_row.innerHTML = element.value;

 

That code gets the value of the element, and puts it in a div.

The last line is probably all you need, editting anything before that with the string will probably defeat the process.

 

What I'm trying to do, is display a certain amount of characters. Lets say 30 for example.

If the string is over 30, reduce by 3, and add "..."

Or whatever will be possible. I know it's possible to do it with php, but not in the way I'm wanting to do it.

"element.value" is the value, so maybe creating a var eg:

var reduced = element.value.reduced to 30; new_row.innerHTML = reduced

However, that's not correct. You may get the idea.

Can anyone help? Thanks.

Link to comment
https://forums.phpfreaks.com/topic/187524-longstring-shortened-down/
Share on other sites

Yeah sorry, that's what I was meant to do.

 

var reduced;
if (element.value.length > 30) {
  reduced = element.value.substr(0, 30)+'...';
} else {
  reduced = element.value;
}

 

Don't really undertsand how you couldn't do that yourself from my example. Javascripts substr() is no different to php's except it is a method of the string object.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.