Javascript obtiene elementos de la lista de una cadena analizada js

I think the best way to approach this is jump straight in and explain as I go.

I am using wordpess with a custom made template (we made it) this code is to deal with the twitter widget pro. (this isn't the prettiest of code granted, I will clean it up later)


  * Sets the twitter area to display: none and pulls in the content from the server
  * Javascript then does a string replace to remove a bit of unwanted text
  * finally javascript will write the doctored string to the client browser

?><div id="twitterRight">

  <ul class="xoxo" style="list-style-type:none; font-size:11px; margin:0px 20px 0 0;">
    <li id="twitter-2" class="widget-container widget_twitter">
    <div id="twitHold" style="display:none;">
    <h3 class="widget-title"><span class='twitterwidget twitterwidget-title'><a href="" target="_blank"><img src="<?php echo home_url(); ?>/images/twitterName.png" width="208" height="27" alt="EhomeS" /></a></span></h3>

    $twitter = dynamic_sidebar('primary-widget-area');
  <div id="twitHolder">

  <script type="text/javascript">
    // Get the posted content from the server
    var str = $('#twitHold').html();
    var x = str.replace("Twitter: @username", "");
    //var shortString = x.substr( 0, 10 );

</div><!-- END #twitterRight -->

The problem is the wordpress function dynamic_sidebar doesn't return a string or anything, only a boolean value so I cannot manipulate that. So what I have done is stored the outputted HTML in a js variable x and manipulated it from there.

What I am trying to achieve is to simply limit the number of characters in each list item (tweets) however I cannot find a way of doing so. I have tried este so far with no luck (im thinking because the javascript is writing it out and parsing it, im not sure).

Is there a way to perform the substr on the list items?

preguntado el 28 de agosto de 12 a las 10:08

1 Respuestas

Question is a little unclear, if you want a collection of the list items then try this.

use a jquery selector, jquery has good documentation

be sure to add the jquery library to your document.

<ul id="twitterlist">
    $twitter = dynamic_sidebar('primary-widget-area');

<script type="text/javascript">
var listitems = $('#twitterlist').children();

Respondido 28 ago 12, 10:08

Hey thanks for the comment, all I am getting on the browser window is [object Object] any reason why? (given you an upvote) - zombi

use the chrome javascript debugger, to have a look inside the javascript execution variables, you should be able to get a clearer picture - lukenz

In he outer html its giving "<script type="text/javascript"> // Get the posted content from the server var str = $('#twitHold').html(); var x = str.replace("Twitter: @designworks_UK", ""); var listitems = $('#twitHolder').children(); document.write(listitems); //var shortString = x.substr( 0, 10 ); //document.write(x); </script>" so I think this is where I am getting stuck, the javascript is parsed rather than actually adding to the "html" or am i wrong? - zombi

use $('#twitholder').html(x) to insert into the DOM - lukenz

Ahh thank you! Now, one final(i hope) question, its outputting [li#twitter-2.widget-container, li, li, li] so.. can i iterate through this and edit the text inside of that then write it back out to the document? Thanks for all your help by the way! Fast edit, if i select an array key eg listitems[0] then it gives me list items: [object HTMLLIElement] so it must be possible some how to do this :S ? - zombi

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.