0

I wish to replace spaces in something like this:

<span>5</span>SPACE<image href="mtgsymbol.png" />

but when I do so using:

card_cost=card_cost.replace(/\s/g,"");

it messes my css all up and the objects stack on each other like their position is changed to absolute.

How do I replace the spaces without breaking my css or document flow?

Example code:

Javascript:

card_cost=document.getElementById('card_cost').value;

if(card_cost)
{
card_cost=card_cost.replace(/\d+/g,"<span class='card_costnum'>$&</span>");
*breaks styling*card_cost=card_cost.replace(/\s/g,"");*breaks styling*
}

document.getElementById('output').innerHTML+="<div class='card'>"+"<span class='card_cost'>"+card_cost+"</span>";

CSS:

.card_name,.card_image,.card_cost,.card_type,.card_subtype,.card_rarity,.card_rules,.card_flavor,.card_strength,.card_num,.card_lower,.card_costnum
{
position:relative;
z-index:1;
}

.card_cost
{
display:inline-block;
float:right;
clear:right;
}

.card_costnum
{
position:relative;
display:inline-block;
text-align:center;
vertical-align:middle;
background-image:url("numsymbol_small.png");
background-repeat:no-repeat;
width:12px;
height:12px;
margin:1px;
top:-4px;
}
7
  • I don't understand - what is it you're trying to do? (I tried answering, but looking back, I'm not sure it's what you need anymore.) Commented Apr 22, 2012 at 17:03
  • Use DOM manipulation to remove whitespace text nodes instead of regex. And don't do .innerHTML+=.... It's a terrible thing to do to your DOM. Commented Apr 22, 2012 at 17:05
  • If you tie to minimize your css you can also remove all new lines and " in the url, etc. have a look at codebeautifier.com Commented Apr 22, 2012 at 17:05
  • The html doesn't match javascript in your example. Commented Apr 22, 2012 at 17:08
  • Whoever posted about positioning the space replacement before any other replacements that fixed my issue. Commented Apr 22, 2012 at 17:11

1 Answer 1

3
card_cost = card_cost.replace(/\d+/g, "<span class='card_costnum'>$&</span>");
card_cost = card_cost.replace(/\s/g, "");

You're turning it into <spanclass='card_costnum'>, which is not valid, and which will break the rest of your HTML. Just reverse the replacements.

card_cost = card_cost.replace(/\s/g, "");
card_cost = card_cost.replace(/\d+/g, "<span class='card_costnum'>$&</span>");
Sign up to request clarification or add additional context in comments.

Comments

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.