tags:

views:

29

answers:

3

I have a variable to the button below that I would like to replace:

<script type="text/javascript">
  var btn = window.document.getElementById('btn_fb_1');
</script>
<button id="btn_fb_1" type="button">Log into Somewhere<br />(Joe Doe)</button>

Using the "btn" variable above, how to I replace the entire element with just text like this (notice I need to keep "Joe Doe":

Connected to Somewhere<br />(Joe Doe)
A: 

JavaScript frameworks make this a lot easier to do, but this should work.

btn.innerHTML = "Connected to Somewhere<br />(Joe Doe)";
Pat
That would set the button's innerHTML to what it was to begin with
mplungjan
That's what I get for not fully reading the question.
Pat
+1  A: 

If the button is wrapped in an object, change the innerHTML of the parent:

<span><button id="btn_fb_1" type="button">Log into Somewhere<br />(Joe Doe)</button></span>
<script type="text/javascript">
  var regEx = /\(([^)]*)\)/
  var btn = window.document.getElementById('btn_fb_1');
  btn.parentNode.innerHTML=btn.innerHTML.match(regEx)[1]; // extract joe doe (no error checking)
</script>
mplungjan
Thx for the help.. how to I extra "Joe Doe" so I can put it where the x's are: "Connected to Somewhere<br />(xxxxx)"
TruMan1
http://stackoverflow.com/questions/1553222/regex-pattern-to-return-text-from-within-brackets
mplungjan
See my amended answer for example with regular expression from link above
mplungjan
+1  A: 

try this code:


var btn = window.document.getElementById('btn_fb_1');
var d = document.createElement('div');
var name = btn.innerHTML.match(/\(.*\)/);
d.innerHTML = 'Connected to Somewhere<br />'+name;
btn.parentNode.replaceChild(d, btn); // replace btn with d

hacksteak25
"Joe Doe" is dynamic though. How do I variablize this
TruMan1
sorry, missed that info ;) I updated the code above
hacksteak25