0

I have something like this

<span style="font-size: 1.7px; letter-spacing: -1.7px; color: #edf6fc">text</span>

and

<span style="font-size: 99.81%; color: #080007">text </span>

How would I select the first one and add; let's say display: none to its style (or remove that span) but not the second one using JavaScript.

Or how do I remove all the span that has CSS property letter-spacing?

3
  • You can use jquery/javascript selectors to select element and add your style. Commented Jul 8, 2013 at 5:55
  • with css selectors you can use 'span:nth-child(2)' but you're almost always better off using a class or ID for this Commented Jul 8, 2013 at 6:00
  • @Arun_C_C how do I select the first type of span but not the second? Commented Jul 8, 2013 at 6:14

3 Answers 3

2

Give class or id to those elements.

.spanOne{
    display: none;
}

or

create those elements dynamically and store them in variables. Then you can use those variables to add or remove properties.

var spanOne = //code to create span element;
var spanTwo = //code to create another span element;

$(spanOne).hide();

or

$('#parentElementID').children('span').eq(0).hide();

UPDATED:

$('div').children('span').filter(function () {
    return this.style.fontSize == "17.7px";  //in your case 1.7px
}).eq(0).css('backgroundColor', 'red');

PS: I recommend adding a class name or id to that element.

Sign up to request clarification or add additional context in comments.

5 Comments

I came cross a website like this. I can't assign a class or id for those span without knowing how to select it.
@TuanAnhTran then add those elements dynamically, as I shown in my answer.
they both have the same parent div.
@TuanAnhTran then you need to check a unique css property value. Something like this jsfiddle.net/RaB67/1
Thank you. I saw you use style.fontSize and did a quick search, there is a DOM style attrib for letterSpacing as well. It works.
0

A simple way would be to add a class name or id:

<span id="iHateYou" style="font-size: 1.7px; letter-spacing: -1.7px; color: #edf6fc">text</span>
<span style="font-size: 99.81%; color: #080007">text </span>

and the js:

document.getElementById('iHateYou').remove();

Comments

0

Use an add on like Firebug for example in Firefox, right click on the tag in issue, like here <span> then select inspect element. Go to the code and find out its parent with a unique identifier (ID), select that parent id and add CSS by parent child selection.

For Instance,

#abc span{display: none;}

Add this in your stylesheet. This will make the child of the unique identifier to attach your style and override that one that is applying to it.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.