-1

I have a string like this:

<!-- Offer Conversion: godaddy --> <iframe src="http://example.go2cloud.org/aff_l?offer_id=90" scrolling="no" frameborder="0" width="1" height="1"></iframe> <!-- // End Offer Conversion --> godaddy

Now I wanna to toggle some parameter into src attribute based on a javascript event.

please see the JsFiddle

3
  • @gaetanoM fiddle seems fine to me Commented Jun 14, 2017 at 11:18
  • @CarstenLøvboAndersen Yes, the link now works. Thanks Commented Jun 14, 2017 at 11:18
  • Sorry, I fixed it. Commented Jun 14, 2017 at 11:18

1 Answer 1

1

To manipulate HTML in string create empty element and set it's content with string. Later do what you want.

var string = '<!-- Offer Conversion: godaddy --> <iframe src="http://example.go2cloud.org/usr_l?offer_id=90" scrolling="no" frameborder="0" width="1" height="1"></iframe> <!-- // End Offer Conversion -->';

$('input').change(function() {
  var content = $('<div>');
  content.html(string);
  var iFrameSrc = content.find('iframe').attr('src');

  if ($('#e1').is(':checked')) {
      iFrameSrc += '&e1='+$('#e1').val();
  }

  if ($('#e2').is(':checked')) {
      iFrameSrc += '&e2='+$('#e2').val();
  }
    content.find('iframe').attr('src', iFrameSrc);

  $('#result').show().text(content.html().replace(/&amp;/g, '&'));
});
#result {
  margin-top: 50px;
  background: #dcfffb;
  color: #044f47;
  padding: 10px;
  font-size: 1em;
  font-family: monospace !important;
  text-align: left;
  direction: ltr;
  border: 1px dotted #9dd3cd;
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
  <input id="e1" type="checkbox" value="someValue1"/>
  <label for="e1">firstParam</label>
</div>

<div>
  <input id="e2" type="checkbox" value="someValue2"/>
  <label for="e2">secondParam</label>
</div>

<div id="result"></div>

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

4 Comments

what is amp; ? I don't need it.
&amp; is the character reference for "An ampersand". stackoverflow.com/questions/9084237/what-is-amp-used-for
I only need &, How can I do this?
@mohammad Simply replace &amp; to &

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.