Peterlu12 Peterlu12 - 2 months ago 11
Javascript Question

How to click each link individually in JS?

Try to separate both links individually by clicking each of them. What should I edit or add?

<script type="text/javascript">
$('#submit').click(function() {
if ($('#user').val() != '') {
alert('For demonstration purposes only. Please do not point your RSS reader to this server.');
var user = $('#user').val();


First One

$('#output')
.html('')
.append('hhtp://www.breakingnews.com/feeds/rss/' +
$('#user').val())
.attr('href', 'http://www.breakingnews.com/feeds/rss/' +
$('#user').val());


Second One

$('#output')
.html('')
.append('https://www.theguardian.com/world/' + user +
'/rss')
.attr('href', 'https://www.theguardian.com/world/' + user + '/rss');

Answer

    var user = $('#user').val();

$('#output')
      .html("")
      .append(jQuery('<a>').attr('href', 'hhtp://www.breakingnews.com/feeds/rss/' +
            user).text('hhtp://www.breakingnews.com/feeds/rss/' + user));
$('#output')
    .append('<br/>')
    .append(jQuery('<a>').attr('href', 'https://www.theguardian.com/world/' + user + '/rss').text('https://www.theguardian.com/world/' + user + '/rss'));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="user" value="user"></input>    
<div id="output"></div>

Problem is in

.html('')

when you add new link you erase all html content added before.

You should to remove this method in second link creation code.

And you should create a link element

Comments