Jump to content

getElementsByClassName


Andy11548

Recommended Posts

Hey, I'm very new to Javascript/jQuery.

 

How would I make this work:

 

     $('.delete_news_button').click(function() {
     var id = document.getElementsByClassName(news_id);

        $('#delete_id_'+id).append(id);
     });

Link to comment
Share on other sites

if your using jQuery, stick with it.

 

$('.delete_news_button').click(function() {
  var id = $('.news_id').val();
  $('#delete_id_'+id).append(id);
});

 

It's hard to give a working example without knowing what the news_id element your trying to refer to actually is. The above assumes it's an input of some type.

Link to comment
Share on other sites

Pretty much, for all the new topics that get placed on the homepage, it creates a form accordingly:

 

<div id="delete_id_'.$newsF['id'].'"></div>
               <form name="news_delete" action="" onsubmit="return false">
                  <input type="hidden" class="news_id" value="'.$newsF['id'].'" />
                  <input type="submit" class="delete_news_button" value="Delete '.$newsF['id'].'" />
               </form></div><br />

 

Each of the Hidden Id's have a value, according to the News_ID. They way you said, I've tried already, but it only works for the top one, not for all of them.

 

I need it to get each ID from the same class value.

Link to comment
Share on other sites

Right, example:

 

<input type="hidden" value="3" class="news_id" />
<input type="hidden" value="7" class="news_id" />
<input type="hidden" value="8" class="news_id" />

 

I want to get each of the ID's from the input fields and put them into a div tag where the hidden ID is the same as the Div ID:

 

<div id="delete_news_3">3</div>
<div id="delete_news_7">7</div>
<div id="delete_news_8">8</div>

 

The Javascript code I'm using is:

 

     $('.delete_news_button').click(function() {
           var id = $('.news_id').val();

           $('#delete_id_'+id).append(id);
     });

 

But, when I click the button "delete" it only does shows the ID appended in the top div tag (<div id="delete_news_3"></div>).

Link to comment
Share on other sites

append appends a child to a parent. You would need to put all your divs in a wrapper and append your new div (which your not currently creating) to that.

 

<div id="stage">
  <div id="delete_news_3">3</div>
  <div id="delete_news_7">7</div>
  <div id="delete_news_8">8</div>
</div>

 

Then:

 

$('.delete_news_button').click(function() {
  $('.news_id').each(function() { // loop through each news_id form element
    var id = $(this).val(); // get the current id
    var div = $('<div />'); // this creates a new div
    div.attr('id', id); // assign the id to the new array
    $('stage').append(div); // append to the stage wrapper.
  });
});

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.