0

need a little help finishing this code;

$(document).ready(function() { 
     var username = $('div.subtitle span').text();
     var $Head = $('div.main-content #field_id4 dt span'); 
     var $body = $('div.main-content #field_id5 dt span');
     var $contBody = $('div.main-content #field_id6 dt span');
     var $borders = $('div.main-content #field_id7 dt span');
$Head.filter(':contains("Black")')
     .closest('#profile-advanced-layout')
      .prepend('<div id="blackbg">'+ username +'Profile</div>');
$body.filter(':contains("Black")')
      .closest('#profile-advanced-layout')
        .css({'background':'#000'});
$contBody.filter(':contains("Black")')
      .find('.main-content')
       .css({'background':'#000'});
$borders.filter(':contains("Black")')
     .find('main-content')
       .css({'border':'2px solid #000'});
 });

My biggest problem here, since this is only maybe third time using variables. Though if you look at the variable $username, what I am trying to do is search inside the div .subtitle span and get the text string that is within that span, then copy it or whatever,

then where is says $Head.filter, I want it to be "+username+Profile", is that correct for the variable to be in placed like that. And how would i go about finding that text string correctly?

EasyBB
  • 6,176
  • 9
  • 47
  • 77
  • to get text inside the element you have to use the .text() method, and not just select the DOM element – Bruno Dec 01 '12 at 18:15

2 Answers2

1

I think you are trying to do this:

.prepend('<div id="blackbg">' + $username.text() + 'Profile</div>');

or if you have the text in a separate variable called username:

.prepend('<div id="blackbg">' + username + 'Profile</div>');
Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
  • ok so thats how I would get that. Now how would I write that in the variable format? var username = .find('.subtitle span').text(); then in the head var I would do +$username+? or still + username.text() + – EasyBB Dec 01 '12 at 18:42
  • @user1760670 once you have text in the string, you can't use `.text()` anymore. It's just a string. If you do `var username = .find('.subtitle span').text();` then you want to use `+ username +`. Note that your code is wrong because you don't close the string. Look at mine. – Explosion Pills Dec 01 '12 at 18:52
  • sorry close the string?? you mean like 'code' + username + 'code' ? – EasyBB Dec 01 '12 at 18:54
  • @user1760670 no; you never assign anything to `username` – Explosion Pills Dec 01 '12 at 19:00
  • I thought I did by doing var $username = $('div.subtitle span').text(); sorry this is new to me so – EasyBB Dec 01 '12 at 19:02
  • Think of `$` like any other letter. There is hardly a difference between a variable named `ausername` and `$username`. It is not functional. Change `$username =` to `username =`. – Explosion Pills Dec 01 '12 at 19:04
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/20453/discussion-between-explosion-pills-and-user1760670) – Explosion Pills Dec 01 '12 at 19:26
0

Try

$username.html(); 

$username.text(); 

Also the $ is not need in a variable name you could do

var username = $('div.subtitle span').html();

See What is the difference between jQuery: text() and html() ?

Community
  • 1
  • 1
MagePal Extensions
  • 17,646
  • 2
  • 47
  • 62