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
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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.