1

How to use JavaScript or CSS to change a string on a page.

I will have around 50 elements which contain tags separated by commas ie

 <a name="tagslist">Skrillex, dubstep, dance</a>

I want to using JavaScript take each of these words and style them differently so that they don't look like they are separated by commas but on different lines. The problem is there can be a variable amount of tag lists on the page.

Is there any way i can achieve this easily?

2
  • This seems like a use case that's not tailor-made for a client-side solution so much as a server-side solution (think PHP). But you should be able to accomplish what you want in Javascript. Commented Feb 10, 2013 at 21:47
  • 1
    do you want each word to be inside its own anchor tag? Or just have 1 anchor tag with the words separated by a line? Commented Feb 10, 2013 at 21:47

5 Answers 5

2

First of all, get out of last milennium and use this HTML:

<div id="tagslist">Skrillex, dubstep, dance</div>

Then you can do this:

var elm = document.getElementById('tagslist');
elm.innerHTML = elm.innerHTML.replace(/, /g,'<br />');

You can of course do something more complex, like this:

elm.innerHTML = "<div class='tag'>"+elm.innerHTML.replace(/, /g,"</div><div class='tag'>")+"</div>";

Alternatively, you can use real DOM methods:

var elm = document.getElementById('tagslist'), tags = elm.firstChild.nodeValue.split(", "),
    l = tags.length, i, div;
elm.removeChild(elm.firstChild);
for( i=0; i<l; i++) {
    div = elm.appendChild(document.createElement('div'));
    div.appendChild(document.createTextNode(tags[i]));
    // apply styles, className, etc. to div
}
Sign up to request clarification or add additional context in comments.

4 Comments

name attributes are last millennium?
@Mark I agree, not sure what Kolink is trying to say. Using id is a worse solution anyway considering the fact that something like a tags list will appear multiple times on the same page.
This works great for the first one, but the rest of my elements still don't work. Is there any way to select all elements with the id tagslist?
In the case of several, the most stable method is to use name attribute and getElementsByName, loop through the node list and apply the code to each one. @Mark <a name> certainly is.
1
$('a[name="tagslist"]').html(function(_, html){
    var arr = [],
        tags = html.split(/\s*,\s*/);

    for (var i=0; i<tags.length; i++)
        arr.push('<div class="whatever">' + tags[i] + '</div>');

    return arr.join('');
});

5 Comments

This would produce invalid HTML. You cannot place block-level elements within inline elements.
True. But this is just sample code. You could use span instead, and make them block via CSS. That would be valid.
Making a span display: block and placing it within an inline element is still invalid.
@w.brian You claim that validity of HTML depends on other things external to HTML like CSS styling? Reference, please. While you're looking for references take a look at this: html5doctor.com/block-level-links-in-html-5
After revisiting the issue and I have come along way from when the question was asked. I have up-voted and voted this the correct solution. All its needs to be perfect is to be JS and of course use SPAN's for in-line display.
0

Consider using an existing library such as http://ivaynberg.github.com/select2/#tags

Comments

0

You would have to have separate anchor tags for each one for them to be styled differently. For line breaks between them, you could put br tags between them.

1 Comment

The items are binded from a JSON feed so i cannot change them before they are inputted into the fields
-1

What you want to do is not possible. A more ideal solution would be to have the html be rendered like so:

<span class="tagslist">
    <a href="#">Skrillex</a>
    <a href="#">dubstep</a>
    <a href="#">dance</a>
</span>

This way, you have complete control over the styling using CSS.

2 Comments

You said you wanted to style individual parts of the text within a tag separately which is not possible without first separating them into separate tags, which is what every else has told you to do using javascript. If you have the ability to generate the ability to generate the HTML yourself, the latter method is incredibly stupid. Suit yourself.
The question was answered partly above. I need to not change the input but to process it. The data is from a feed which i don't control. Thanks for work put into the answer though! :)

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.